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.
melegant99Asked:
Who is Participating?
 
nsaxelbyConnect With a Mentor Commented:
You do not need the ref keyword. I dont understand your last question. You probably do not need to think about the memory address. When you tell it to point to the object, it will always point to that object no matter what you do to it. I hope that answers your question.
0
 
HainKurtSr. System AnalystCommented:
when you create the class WorkMethods, you should create a new instance and set the property, or create a new object and set it using property

void new(){
 mji = new SaleInfo;
}

or

WorkMethods wm = new WorkMethods();
SaleInfo si = new SaleInfo();
wm.mJi = si;
0
 
melegant99Author Commented:
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.
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
nsaxelbyCommented:
It is a copy. So any changes made to any instance of it will change every instance.
0
 
nsaxelbyCommented:
Forgive my use of he word 'copy'. It is a reference, pay attention to the latter part of my comment.
0
 
melegant99Author Commented:
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(myObect)

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


0
 
melegant99Author Commented:
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?
0
 
nsaxelbyCommented:
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.
0
All Courses

From novice to tech pro — start learning today.