Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Passing Objects

Posted on 2004-08-31
1
Medium Priority
?
192 Views
Last Modified: 2010-04-15
HI All,

This is a simple question, i m confused about.

I am passing an object from one class to another like,

Class1.cs

\\ Make Object of Class3 - suppose obj3
\\ Call Class2 passing object of Class3

Class2 secondClass = new Class2(obj3);


Now in Class2.cs constructor

private Class3 objof3;

Class2(Class3 anObject)
{
objof3 = anObject;
// Some Stuff
}

I want to use the object values of anObject throughout my Class2 class.
So, i am making private object of class3 in Class2 itself. and assign that objof3 to the anObject object.

Now the question is,
If i am assigning it this way, then will objof3 will contain the reference to the anObject object, or it will copy everything in a new memory location.

as far as i know, objects are reference types, and so it will take only the reference of the anObject class, and i will be able to use the anObject values throughout my Class2 through the objof3 object, is it right??

I just want to clear my doubt.
0
Comment
Question by:chintan_vaishya
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 

Accepted Solution

by:
monkeymole earned 500 total points
ID: 11939726
Yes you are right with the reference model you describe. You will only store a refernce to the object in Class2. Therefore if you created multiple Class2's with the same instance of Class3 object, any changes to the instance used to create the Class2s would be seen in all the Class3's.
0

Featured Post

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

660 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question