473,224 Members | 1,345 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,224 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 1844
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...
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...

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.