C# class library: insatnce or reference?

B

Bedny

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
 
M

Michael Bray

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
 
I

Ignacio Machin \( .NET/ C# MVP \)

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,
 
J

Jon Skeet [C# MVP]

Bedny said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top