GoD oF SouL Posted September 30, 2009 Share Posted September 30, 2009 Is there a way to assign the "ref" of a variable to another variable in C#? I mean, passing the "ref" of a variable through a method is ok, but can we then assign that "ref" to another variable (a global var) ? Link to comment Share on other sites More sharing options...
0 BGM Posted October 1, 2009 Share Posted October 1, 2009 there isn't really a concept of global variable in c# what are you trying to achieve? Link to comment Share on other sites More sharing options...
0 GoD oF SouL Posted October 1, 2009 Author Share Posted October 1, 2009 (edited) Actually, Im using Visual C# 2005. And global variable or not, what I want is to put the ref (not value) of a var into another var. E.g: var1 = (ref) var2 Edited October 1, 2009 by GoD oF SouL Link to comment Share on other sites More sharing options...
0 BGM Posted October 1, 2009 Share Posted October 1, 2009 ok, well this should do the trick.. protected void Button1_Click(object sender, EventArgs e) { string x = "something"; doStuff(ref x); Label1.Text = x; // this will be "somethingelse" } void doStuff(ref string s) { s = "somethingelse"; } Link to comment Share on other sites More sharing options...
0 coderomega Posted October 1, 2009 Share Posted October 1, 2009 why not use a property? : private string one; private string two; public string One //notice Upper case { get { return one; } set { one = value; two = value; } } public string Two { get { return two; } } so instead of using "one" directly, use "One" which will automatically write the same value into the strings "one" and "two" Also if you wish to read the value of "two", use instead the "Two" property which is read only Link to comment Share on other sites More sharing options...
0 GreenMartian Posted October 1, 2009 Share Posted October 1, 2009 Maybe you're looking for Pointers? http://www.google.com/search?q=c%23+pointers (sorry, haven't really got the time to prune the results, and I really haven't played around with pointers in C#) Link to comment Share on other sites More sharing options...
0 Antaris Veteran Posted October 1, 2009 Veteran Share Posted October 1, 2009 The problem you are seeing is one of the differences between value types and reference types. Ignoring the 'ref' concept for now, you should look at what makes a value type, and what makes a reference type. Reference types don't actually store the intended value, but they store the memory address of that intended value. That way you could do something like this: MyClass file1 = new MyClass() { Text = "Hello"; } MyClass file2 = file1; file1.Text = "World"; Console.WriteLine(file2.Text); And expect the output of Console.WriteLine(...) to be "World" because file1 and file2 point to the same memory location. Value types don't do this: int number1 = 2; int number2 = number1; number1 = 74; Console.WriteLine(number2); That should output the value 2, not the value 74, as the variable number2 does not hold a memory address, but its own value. As GreenMartian has said, pointers would be the way to do this, so I could do the following: int *number1 = 2; int *number2 = &number1; *number1 = 74; Console.WriteLine(number2); And I would expect the output of variable number2 to be 74, as it is a pointer to the other variable number1 (well, a pointer to its memory address). This is generally regarded as bad practice, as it is invoking unmanaged/unsafe code. C# (well .NET) employs an efficient memory manager and garbage collector, which will not manage [the disposal of] the pointers and free up the used memory for you. There is also a couple of other side effects: You can only use pointers in code blocks marked as unsafe. Unsafe code can only be run in Full-trust. Don't get the concept of reference/value types mixed up the the ref/out modifiers in method parameters. They are very different things. Link to comment Share on other sites More sharing options...
0 Andre S. Veteran Posted October 1, 2009 Veteran Share Posted October 1, 2009 (edited) I was pretty mixed up with reference types when I started C#, so take your time to understand them. Antaris' explanation is perfect. What happens when you are passing a reference type to a function, is that you are making a copy, not of the object, but of the reference to that object. There are now two references to the same object: one inside the function call, and one outside. Both can be used to read and modify the same object, so any modifications to the object in the function will be seen by the caller when the function returns. However, if you reassign the copy to another object, the original reference will still point to the original object. In C#, object references are passed by value. If you can understand that, you've understood everything. This is where ref comes in. Ref allows you to pass object references by reference, so any changes made to the reference modify the original too. Let's illustrate this with an example: using System; namespace ConsoleApplication1 { class MyClass { public string Text { get; set; } } class Program { static void Main(string[] args) { var obj = new MyClass() { Text = "Hello" }; Console.WriteLine(obj.Text); modifyObject(obj); Console.WriteLine(obj.Text); modifyReference(ref obj); Console.WriteLine(obj.Text); } static void modifyObject(MyClass b) { b.Text = "World"; } static void modifyReference(ref MyClass c) { c = new MyClass() { Text = "Rabbit" }; } } } This outputs, as intended: Hello World Rabbit If we remove the ref keyword, this outputs: Hello World World Can you see why now? Edited October 1, 2009 by Dr_Asik Link to comment Share on other sites More sharing options...
0 GoD oF SouL Posted October 2, 2009 Author Share Posted October 2, 2009 Thanks for all your answers. I studied them and came to the conclusion that: Although I also thought of pointers, it would be an unnecessary complexity since a simpler approach could be taken. Thx. Link to comment Share on other sites More sharing options...
Question
GoD oF SouL
Is there a way to assign the "ref" of a variable to another variable in C#?
I mean, passing the "ref" of a variable through a method is ok, but can we then assign that "ref" to another variable (a global var) ?
Link to comment
Share on other sites
8 answers to this question
Recommended Posts