C #: save ref parameter from constructor in class

Basically, I would like to have a reference to a variable inside the class instance, but I would like the link to become a class variable, so I do not need to send it inside the class as a parameter

the code:

int num = 0; myClass(num); print num; // output is 0 but i'd like it to be 10 :) class myClass { private int classNumber; myClass(ref int number) { print number; //output is 0 // id like this to be a reference to the refenrence classNumber = number; DoSomething(); } public void DoSomething() { ClassNumber = 10; } } 

Why am I asking about this because I work with winforms and has a main form sending a class instance to a new form, which should edit the class and send it back. Now I use Form.ShowDialog () so that the user does not use the main form when editing in a new form and after that captures data from new forms

 editForm edtfrm = new editForm(ref instanceOfClass); edtfrm.showDialog(); //grab the instance back instanceOfClass = edtfrm.editedClass; 

How can i solve this? I do not like this decision

+7
source share
6 answers

I would like to be able to refer to a variable inside an instance of the class, but I would like the link to become a class variable, so I don't need to send it inside the class as a parameter

Then you have to live with disappointment. A CLR system explicitly prohibits storing variable references as members of classes. CLR allows you to refer to variables as

  • passed to methods as arguments corresponding to formal parameters, or 'this'
  • saved as local
  • returned as method return values

but does not allow storing in arrays, fields, etc. In principle, everything that goes "a bunch" cannot contain links.

C # provides the first function: refers to variables as method parameters. It does not reveal two other functions (although I wrote an experimental version of C # that works, and it works pretty well.)

Note that C # does not allow refs to be used in contexts that require storage of the ref ref heap, for example, the ref parameter, which is a private external lambda variable, for example. There are a few rare cases where the compiler resolves what looks like long-term storage of ref and uses copy-in-copy-out semantics to emulate a link, but it's probably best not to go there.

Why does the CLR have this limitation? The right way to think about the fact that there are two types of storage: long-term and short-term, usually called "heap" and "stack". But the form of the data structure does not matter; length of life matters. The variable has a storage location; what a variable. If you can keep a reference to a variable extracted from a short-term storage in a long-term storage, then a long-term storage stores a link to something that has a shorter lifespan, and therefore it may crash and die when accessing a variable after its death.

Obviously, there are many ways to solve this problem. For example, the CLR team could make it illegal to get a short-term storage link and allow link storage in long-term storage. But this means that you cannot accept references to local variables or parameters that you would like to place in the short-term storage, because their life is so short.

The way the CLR team actually chose is to prohibit the long-term storage of any links. Like any design decision, this was the result of many trade-offs against competing goals.

+14
source

It is not a good idea what you are trying to do, I would expose the modified object as a property of the class, as shown below:

 public class ClassContructorReference { static void Main(string[] args) { object var = new object(); MyClass myClass = new MyClass(var); StringBuilder mySb = myClass.Instance as StringBuilder; Console.WriteLine(mySb.ToString()); } } public class MyClass { public object Instance {get;set;} public MyClass(object var) { this.Instance = var; DoSomething(); } private void DoSomething() { this.Instance = new StringBuilder("Hello"); } } 
+2
source

Of course, your test code will not work, as it is a primitive type. but your second code will work as it is a reference type. (even "ref" is not required). No need to assign an instance back.

 public class Second { public First f; public Second(First f) { this.f= f; } public void change() { this.f.Name = "PUli"; } } public class First { private string _name; public First() { Name = "SUli"; } public string Name { get { return _name; } set { _name = value; } } } class Program { static void Main(String[] args) { First f = new First(); Second sec = new Second(f); Console.WriteLine(f.Name); sec.change(); Console.WriteLine(f.Name); } } 

Output: -

Suli

Bullets

+1
source

Create a class containing your number as a property and pass it by your logic. This class will represent your "Model".

0
source

You cannot save the ref parameter, a ref not a reference, it is just alias . If you have:

 public void Stuff (ref int i) { i = 2; } 

and name it:

 int s = 1; Stuff(ref s); 

ref means "make an alias for s and extend it to it." As soon as you leave the scope of the method, this alias has disappeared. By the way, Eric Lippert started a series about it on the blog .

You must create a class and use it in your logic. The GUI should not manipulate the values, only the backend should.

0
source

A few things here. First, in your constructor, you probably want to do

 DoSomething(); number=classnumber; 

instead

 classnumber=number; 

Second try

 myClass(ref num); 

instead

 myClass(num); 
0
source

All Articles