.net - C# Pointers of managed types -


depending on parameter in method, change different variables in class , act on them. in c++ super easy, in c# seems more difficult without lot of if/else statements. there better way in c#?

in c++ (its been few years since coded in c++ kind):

void mymethod(int option) {     int* _i;     string* _s;     myclass* _mc; // created class     datagridviewcolumn _col; // managed class      if(option == 0)     {         _i = &m_someint;         _s = &m_somestr;         _mc = &m_somemc;         _col = &m_somecol;     }     else if(option == 1)     {         _i = &m_someotherint;         _s = &m_someotherstr;         _mc = &m_someothermc;         _col = &m_someothercol;     }      // can act on _i, _s, etc , im acting on member variables.     _i = 5;     _s = "changed string";     ..... } 

this want do, in c#. solution , messy @ end:

void mymethod(int option) {     int _i;     string _s;     myclass _mc; // created class     datagridviewcolumn _col; // managed class      if(option == 0)     {         _i = m_someint;         _s = m_somestr;         _mc = m_somemc;         _col = m_somecol;     }     else if(option == 1)     {         _i = m_someotherint;         _s = m_someotherstr;         _mc = m_someothermc;         _col = m_someothercol;     }      _i = 5;     _s = "changed string";     .....      if(option == 0)     {         m_someint = _i;         m_somestr = _s;         m_somemc = _mc;         m_somecol = _col;     }     else if(option == 1)     {         m_someotherint = _i;         m_someotherstr = _s;         m_someothermc = _mc;         m_someothercol = _col;     } } 

in c# need wrap them in container , choose between 2 containers

class datacontainer {     public int {get; set;}     public string s {get;set;}     public myclass mc {get;set;}     public datagridviewcolumn col {get;set;} }  void mymethod(int option) {     datacontainer container;          if(option == 0)     {         container = m_somecontainer;     }     else if(option == 1)     {         container = m_someothercontainer;     }     else     {          throw new argumentoutofrangeexception(nameof(option));     }      container.i = 5;     container.s = "changed string";     .....  } 

a better option don't take in option , instead pass in container class itself.

void mymethod(datacontainer container) {     container.i = 5;     container.s = "changed string";     .....  } 

Comments

Popular posts from this blog

javascript - Chart.js (Radar Chart) different scaleLineColor for each scaleLine -

apache - Error with PHP mail(): Multiple or malformed newlines found in additional_header -

java - Android – MapFragment overlay button shadow, just like MyLocation button -