473,386 Members | 1,842 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

C# class library: insatnce or reference?

Hello,
i'm new to C# and I try to create class library. Everything looked fine
till the moment I started to use instance of some class like a property
of another class.
Example:
There's class ClassA with public props int A, int B. Also I have class
ClassB with public props int C, int D, ClassA clsA.

I create one object per each class like this:

MyNamespace.ClassA objClassA = new MyNamespace.ClassA();
MyNamespace.ClassB objClassB = new MyNamespace.ClassB();

objClassB have public property objClassB.clsA. When I set
value to objClassA.A to 2 I see the same value in objClassB.clsA.A.

Why that is? How so?

It seems that i work with references, not with instances.
ClassB has constuctor where clsA is created using code
MyNamespace.ClassA clsA = new MyNamespace.ClassA()
so I thought it is new instance of class ClassA.

Can anybody help me what to do or explain me wha that is?

Thanx a lot, Bedny

Dec 19 '05 #1
5 1847
Please provide the code you're using.

--
Venlig hilsen
Anders Borum / SphereWorks
Microsoft Certified Professional (.NET MCP)
Dec 19 '05 #2
"Bedny" <to**********@seznam.cz> wrote in news:1134997965.580814.98810
@o13g2000cwo.googlegroups.com:
objClassB have public property objClassB.clsA. When I set
value to objClassA.A to 2 I see the same value in objClassB.clsA.A.

Why that is? How so?

It seems that i work with references, not with instances.
ClassB has constuctor where clsA is created using code
MyNamespace.ClassA clsA = new MyNamespace.ClassA()
so I thought it is new instance of class ClassA.


It sounds like the ClassA.A might be declared as 'static'. If that is the
case, then ANY instance of ClassA will always report the same value for
this variable. If you want to have separate values of 'A' for each
instance of ClassA, make sure that A is not 'static'.

-mdb
Dec 19 '05 #3
Hi,

I create one object per each class like this:

MyNamespace.ClassA objClassA = new MyNamespace.ClassA();
MyNamespace.ClassB objClassB = new MyNamespace.ClassB();

objClassB have public property objClassB.clsA. When I set
value to objClassA.A to 2 I see the same value in objClassB.clsA.A.


Are you using static for objClassA.A?

post your code

cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation


Dec 19 '05 #4
Bedny <to**********@seznam.cz> wrote:
Hello,
i'm new to C# and I try to create class library. Everything looked fine
till the moment I started to use instance of some class like a property
of another class.
Example:
There's class ClassA with public props int A, int B. Also I have class
ClassB with public props int C, int D, ClassA clsA.

I create one object per each class like this:

MyNamespace.ClassA objClassA = new MyNamespace.ClassA();
MyNamespace.ClassB objClassB = new MyNamespace.ClassB();

objClassB have public property objClassB.clsA. When I set
value to objClassA.A to 2 I see the same value in objClassB.clsA.A.

Why that is? How so?

It seems that i work with references, not with instances.
ClassB has constuctor where clsA is created using code
MyNamespace.ClassA clsA = new MyNamespace.ClassA()
so I thought it is new instance of class ClassA.

Can anybody help me what to do or explain me wha that is?


As others have said, more code would help. However, I suspect you're
expecting value type semantics, when you're actually dealing with
reference types. I'm still trying to find the time to write up types
properly, but you may find the following articles useful:

http://www.pobox.com/~skeet/csharp/memory.html
http://www.pobox.com/~skeet/csharp/parameters.html

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Dec 19 '05 #5
Thanx a lot. Bedny

Dec 23 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: martin | last post by:
Hi, I have created a class that is totally seperate from my web application. However this class is used extensivly by the web application for stuff like data access. I wish to add a function to...
4
by: Brian Shannon | last post by:
I am playing around with class libraries trying to understand how they work. I created a class library, library.vb. I placed the library.dll into the bin directory and set my reference. If I...
2
by: George Durzi | last post by:
Consider these three projects: - ProjectWeb (my web application) - Facade (my class library, does data access, etc.) - Library (a library of classes I reuse in applications) ProjectWeb...
6
by: ryan.d.rembaum | last post by:
Hello, I have code that I wish to use in many web applications. Basically sort of stand utility stuff. So from Visual Studio Project I select add a component and chose Component Class. Lets...
7
by: John | last post by:
Hi I open a word document from my vb.net app. Now I want to pass reference to one of the classes in my vb.net app to word so it can access procedures in the vb.net class. How can I pass the...
5
by: Tony Johansson | last post by:
Hello! I have one solution file that consist of three project. One project that build the exe file called A One project that build a user control dll. Here we have a class called B One project...
0
by: tony | last post by:
Hello! I have one solution file that consist of three project. One project that build the exe file called A One project that build a user control dll. In this user control we have a class...
5
by: tony | last post by:
Hello! This is a rather long mail but it's a very interesting one. I hope you read it. I have tried several times to get an answer to this mail but I have not get any answer saying something...
0
by: russell.lane | last post by:
I have a web service that exposes database contents as disconnected datasets. I would to use that web service from within a class library. I have included a web reference to the service in my...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.