Maintain a reference [c#]  
Author Message
Rodolfo Navarro





PostPosted: Visual C# General, Maintain a reference [c#] Top

How can I mantain a reference to a var .. i don't mean only pass a var in reference mode to a method, i want to mantain that reference...

Here's what i want to do...

I have a Form1 class, so i have a var, call it int X, then i want to access to that var exactly how she is in the Form2 class.. (but i dont want to pass a copy.. i want that if i modify that var in form2 the form1 get the change too.. )

Best Regards




Visual C#17  
 
 
m0





PostPosted: Visual C# General, Maintain a reference [c#] Top

Hi,
There is attribute called ref

int hi = 4;
Console.WriteLine(myfunction(hi));

void myfunction(ref int test) {
test = 3;
}

www.c-sharpcorner.com/Language/out_and_ref.asp

Enjoy :)


 
 
ahmedilyas





PostPosted: Visual C# General, Maintain a reference [c#] Top

not sure I quite follow, but you could also try using the out keyword in the method, which means that when you pass the parameter as an out reference, it will modify the parameter in the method and the caller will have that value modified also. Example:

 

//caller:

int myValue = 2;

this.DoSomething(out myValue);

//myValue will be 1 now, since it was modified in the method, DoSomething.

 

private void DoSomething(out int someparameter)

{

   someparameter = 1;

}

 

you may also find this interesting:

http://www.c-sharpcorner.com/Language/KunalRefOut.asp



 
 
Rodolfo Navarro





PostPosted: Visual C# General, Maintain a reference [c#] Top

Hi, and thanks about the answer... but.. i know abot the ref :(.. the thing is that...

int hi = 4;
Console.WriteLine(myfunction(hi));

void myfunction(ref int test) {
test = 3;
}

I want to mantain the pointer or reference to 'hi'.. for example... suposse hi is defined in the form1 class, and then..

myfunction is a method from form2 class.. so ok i pass the ref int test as a hi reference, but i want to mantain a pointer in the form2 class just to keep using that values (that may change, that's the reason why i don't want copy of the value.. so.. )

Form1 class

int hi = 4;

Form2 class

int reference;

void myfunction(ref int test) {
reference = test;
}

Test is a pointer.. but.. at that point (reference = test;) c# makes a copy of the value, so, reference var is not a reference anymore.. that reference is only for that function scope.. i want to maintain that pointer, i want to use that arround the form2 class.

Thanks about the answers

Best Regards



 
 
RizwanSharp





PostPosted: Visual C# General, Maintain a reference [c#] Top

The thing you are trying to achieve! I do it in alternat way. I create a public static class in my project and define all fields in it which have to be shared in diffrent classes. Make public fields around those private fields and then get and set from different pieces of code. This ismy own approach I dont know if it does convince you are not. But I actually get great results as what you are expecting in your problem.

Best Regards,



 
 
V.Tortola





PostPosted: Visual C# General, Maintain a reference [c#] Top

You can box the value in a object, but is not recomended. On this way, when you asing the variable ... you copy the reference (like a pointer) that point to same object in the heap , but you must unbox the value before can use it, that's :

int hi = 4;

object o = hi; // box

// if you change 'hi' variable, 'o' don't change but ...

object a =o;
object b=o;
object c=o;

(int)hi = 6 // unbox

// now a, b and c are 6

But you must avoid do boxing/unboxing, because is overheater. You must follow the RizwanSharp instructions, use static fields is the elegant way.

Regards.