melegant99
asked on
Possible for C# Property to hold a reference?
Hello,
I have a class called let's say SaleInfo. Now, I have another class called WorkMethods.
There is a thrid Class called SomeForm that actually creates the original instance of SaleInfo.
I want to create a property in WorkMethods to hold the an instance of SaleInfo.
-- in WorkMethods class
SaleInfo _j;
public SaleInfo mJi { get { return _j; } set { _j = value; } }
Now, isn't this a COPY of the SaleInfo instance? If I make changes it to here, will it be seen by anotehr class where the class is being instaciated? Do I need to use the ref keyword? Or is what I want not possible?
Thanks.
I have a class called let's say SaleInfo. Now, I have another class called WorkMethods.
There is a thrid Class called SomeForm that actually creates the original instance of SaleInfo.
I want to create a property in WorkMethods to hold the an instance of SaleInfo.
-- in WorkMethods class
SaleInfo _j;
public SaleInfo mJi { get { return _j; } set { _j = value; } }
Now, isn't this a COPY of the SaleInfo instance? If I make changes it to here, will it be seen by anotehr class where the class is being instaciated? Do I need to use the ref keyword? Or is what I want not possible?
Thanks.
ASKER
Well, I want the proprerty in the WorkMethods class because I am creating the instance of SaleInfo class well before WorkMethods is ever created.
Also, part of my question above is that if I were to in the third class SomeForm say:
workMethodInstance.mJi = currentMji;
is the property now a copy, or a reference? if it is not a reference, is there a keyword I can use that would make it a reference.
Thanks.
Also, part of my question above is that if I were to in the third class SomeForm say:
workMethodInstance.mJi = currentMji;
is the property now a copy, or a reference? if it is not a reference, is there a keyword I can use that would make it a reference.
Thanks.
It is a copy. So any changes made to any instance of it will change every instance.
Forgive my use of he word 'copy'. It is a reference, pay attention to the latter part of my comment.
ASKER
With that being said, I was reading this article nsaxelby and what I take from it is that though C# passes object types as a reference by default, it looks that it makes a copy of that reference. So in a method call for instance if you want a method in some other class to work on the object you are passing it you would say:
defined:
someMethod(ref SaleInfo)
call
someClass.someMethod(ref myObject)
vs
someClass.someMethod(myObe ct)
So long story short, is that property keeping the actualy memory address of the original class instance, or just a copy of that memory address?
http://en.csharp-online.net/Common_Type_System%E2%80%94Pass-by-Value_and_Pass-by-Reference
defined:
someMethod(ref SaleInfo)
call
someClass.someMethod(ref myObject)
vs
someClass.someMethod(myObe
So long story short, is that property keeping the actualy memory address of the original class instance, or just a copy of that memory address?
http://en.csharp-online.net/Common_Type_System%E2%80%94Pass-by-Value_and_Pass-by-Reference
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
It does answer my question, but what I was getting at is that with a method call and passing an object, work done on that object is not maintained from the calling class unless you use the ref keyword, correct?
Nope, work done on that object is maintained globally even without the ref keyword. There is no need to use ref with C# 99% of the time.
void new(){
mji = new SaleInfo;
}
or
WorkMethods wm = new WorkMethods();
SaleInfo si = new SaleInfo();
wm.mJi = si;