c# - How to use a static utility method for property setters in a utility class -


i'm trying achieve two-way binding between datagridview , bindinglist provides data dgv. columns not yet reflect changes in underlying list , think it's because have not provided property setter(s) notify of property changes. rather code setter rows property same way did process property, i'm trying more "elegant" , realize stuck....

i stumbled upon interesting writeup more elegant approach , i'm trying implement concepts of (please see): http://www.gavaghan.org/blog/2007/07/17/use-inotifypropertychanged-with-bindinglist/

here code mike's article want use (established utilities.cs in cbmi.common project):

using system; using system.collections.generic; using system.componentmodel; using system.linq; using system.text; namespace cbmi.common { public static class utilities {     public static bool set<t>(object owner, string propname,         ref t oldvalue, t newvalue, propertychangedeventhandler eventhandler)     {         // make sure property name exists         if (owner.gettype().getproperty(propname) == null)         {             throw new argumentexception("no property named '" + propname + "' on " + owner.gettype().fullname);         }         if (!equals(oldvalue, newvalue))  // raise event if value has changed         {             oldvalue = newvalue;             if (eventhandler != null)             {                 eventhandler(owner, new propertychangedeventargs(propname));             }         }     return true;    // please note: had add statement avoid compile error:         // "not code paths return value".     } } } 

so, first question this: author did not have return statement in article , added resolved compiler error. i'm guessing eventhandler executes , returns , author omission , should return true method wants bool return type. correct assumption?

my 2nd question shows c# rookie when try use helper method above. have coded class separate file called inputfileinfo.cs in same project (and namespace) above:

    using system; using system.collections.generic; using system.componentmodel; using system.linq; using system.text; namespace cbmi.common {     public class inputfileinfo : inotifypropertychanged     {     private bool processthisfile;     public bool process     {         { return processthisfile; }         set         {         processthisfile = value;         this.notifypropertychanged("process");         }     }      public string filename { get; set; }      private long rowsreturned;     public long rows     {         { return rowsreturned; }         set         {         utilities.set(this, "rows", ref rowsreturned, value, propertychanged);         }      }     public string message { get; set; }       // constructor     public inputfileinfo(string fname)     {         process = true;         filename = fname;         rows = 0;         message = string.empty;     }      public event propertychangedeventhandler propertychanged;      private void notifypropertychanged(string name)     {         if (propertychanged != null)         propertychanged(this, new propertychangedeventargs(name));     }     } } 

the setter 2nd property in class try use mike's static method:

utilities.set(this, "rows", ref rowsreturned, value, propertychanged); 

if remove utilities.set , code follows:

set(this, "rows", ref rowsreturned, value, propertychanged); 

..then compiler complaining "the name 'set' not exist in current context".

i tried adding using utilities; directive did not fix problem.

finally, not understand parameters: ref t oldvalue, t newvalue
nor parameter called value set method invoked.

can please me on these multiple confusions code can use these more advanced ideas?

---- edit update ---- 2 answers helped me working. "2nd question" in original post above remains bit elusive. added comments each requesting "best practice" on how package can use simple invoking syntax in mike's original article. is, i'm seeking invoke "helper" static methods method name only. want understand how invoke like:

set {     set(this, "rows", ref rowsreturned, value, propertychanged); } 

instead of having code as:

set {     utilities.set(this, "rows", ref rowsreturned, value, propertychanged); } 

i got working coding utilities.set guess question morphs bit - "where put static methods , how call them don't have "qualify" them classname?" understand how package useful "utility" type methods don't require instance of object. in case, static method called set i'd able add other static methods such as:

public static int helpfulmethodxxxx(string s, int num) 

i have separately compiled dll (vstudio project) containing class file(s). ultimately, i'd think use class in other applications.

where best place declare these sort of static methods such invoked as:

int = helpfulmethodxxxx("sample", testnumber); 

instead of:

int = containingclassname.helpfulmethodxxxx("sample", testnumber); 

1: non-void methods need have explicit return statements.

2: cmbi.common namespace. utilities name of class. set() function of class.

the call set() makes sense in context of utilities class. set() not part of global namespace - such, if want call set() outside of utilities class, have specify want utilities.set(), opposed somethingelse.set(). (inside utilities, compiler understands set() refers utilities.set()).

using statements can include namespaces (cmbi.common), or specific classes inside namespaces, if don't want every class in namespace (cmbi.common.utilities). cannot, however, turn class function global function.

3: t refers name of generic type function operates on. http://msdn.microsoft.com/en-us/library/ms379564(v=vs.80).aspx

generics allow same code manipulate, say, collection of integers, , collection of strings, while enforcing compile-time type safety (the compiler give error, if try push integer collection of strings.)

ref means parameter passed reference - , changes made parameter within body of function propagate parameter's value in context of function's caller.


Comments

Popular posts from this blog

linux - Using a Cron Job to check if my mod_wsgi / apache server is running and restart -

actionscript 3 - TweenLite does not work with object -

jQuery Ajax Render Fragments OR Whole Page -