471,334 Members | 1,363 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,334 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 1782
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by martin | last post: by
4 posts views Thread by Brian Shannon | last post: by
2 posts views Thread by George Durzi | last post: by
6 posts views Thread by ryan.d.rembaum | last post: by
7 posts views Thread by John | last post: by
reply views Thread by russell.lane | last post: by

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.