467,134 Members | 1,043 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,134 developers. It's quick & easy.

C# - Basic Question

Hey guys, I know this is probably a very easy, basic question, so feel free to direct me to a detailed answer. I have done some searches some some of the information I get is vague.

In C#, the basic idea I understand is to store information in objects to be used by other objects later. So to say building blocks of the program. My question is this. If I Make Object A, and then Object B, and in Object B I set a variable (can't think of a way to say it better) to equal Object A does this "copy" or "point" to Object A from Object B?

I ask because I am confused, if I make changes in Object A from the reference in Object B, is the orginal Object A going to inherit those changes as well?

Thanks
Aug 6 '08 #1
  • viewed: 1176
Share:
3 Replies
Plater
Expert 4TB
So class definitions:
Expand|Select|Wrap|Line Numbers
  1. public class objA
  2. {
  3.    public int SomeVal=0;
  4. }
  5.  
  6. public class objB
  7. {
  8.    public objA myobjA;
  9. }
  10.  
Then
Expand|Select|Wrap|Line Numbers
  1. objB myobjB = new objB();
  2. objA myoA = new objA();
  3.  
  4. myobjB.myobjA=myoA;
  5. //myobjB.myobjA IS now myoA, I believe any changes to myoA would be reflected in myobjB.myobjA, and vice-versa
  6.  
I would think a tutorial on Object Oriented Programming (OOP) would be able to explain it better then I can.
Aug 6 '08 #2
It is like Plater said, if you say

objb.obja = obja

all that objb.obja is getting is a pointer to obja, and any changes to objb.obja will be reflected in obja.

If you do not want just a pointer, you would have to do something like

objb.obja = new obja(obja.values n' such).

You would still have objb.obja AND obja and they would be two different objects.

Hope that's what you were looking for.
Aug 6 '08 #3
that is exactly what I was looking for... Thanks very much
Aug 6 '08 #4

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

6 posts views Thread by pauldepstein@att.net | last post: by
9 posts views Thread by Malcolm | last post: by
4 posts views Thread by Ramesh | last post: by
5 posts views Thread by Aussie Rules | last post: by
4 posts views Thread by MikeB | last post: by
3 posts views Thread by Scott Stark | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.