c# - How to set Out parameter in method in same class -


  main()    {     bool flag = false;     classa obj = new classa(out flag);      if(flag==true)     {         //do smething     } }  classa {     classa(out bool flag)     {        flag = false;           }     private void someclickevent()     {          flag = true;     } } 

i passing bool value constructor. need set value in someclickmethod() same value return out parameter how can do?

value types passed value. if want keep reference around in class , modify in method , see changes reflected outside, either expose property or introduce container class, passed reference.

so either expose property:

public class foo {     public bool somevalue { get; }      public foo(bool somevalue)     {         somevalue = somevalue;     }      public void foo2()     {         somevalue = true;     } } 

where call this:

bool flag = false; var obj = new foo(flag); // ... flag = obj.somevalue; 

or create container hold value:

public class boolcontainer {     public bool somevalue { get; set; } }  public class foo {     private boolcontainer _boolcontainer;      public foo(boolcontainer boolcontainer)     {         _boolcontainer = boolcontainer;     }      public void foo2()     {         _boolcontainer.somevalue = true;     } } 

where call this:

var boolcontainer = new boolcontainer(); var obj = new foo(boolcontainer); // ... bool flag = boolcontainer.somevalue; 

now caller can access boolcontainer.somevalue inspect (or alter) value.


Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -