c# - Interface Object Always Null -


i created "personfuntionality" object "personfuntionality" interface. interface has method save details of person.the problem personfuntionality has null value.

public personfuntionality personfuntionality;     try             {                 list<beans.person> prsn = new list<beans.person>();                  beans.person psn = new beans.person();                  psn.personid = convert.toint32(txtid.text.trim());                 psn.personname = txtname.text.trim();                 psn.personcity = txtcity.text.trim();                  prsn.add(psn);                  //prsn.personid = convert.toint32(txtid.text.trim());                 //prsn.personname = txtname.text.trim();                 //prsn.personcity = txtcity.text.trim();                  if (personfuntionality != null)                 {                     bool success = personfuntionality.savepersondetails(prsn);                      if (success == true)                     {                         lblresult.text = "success";                     }                     else                     {                         lblresult.text = "failed";                     }                 }                 else                 {                     lblresult.text = "object null";                 }             }             catch (exception ex)             {                 throw new exception(ex.message);             } 

you never instantiated personfuntionality object in give code declared it. can instantiate under. assume have parameter less constructor class personfuntionalityimlementorclass implemented personfuntionality interface.

public personfuntionality personfuntionality = new  personfuntionalityimlementorclass(); 

you have interface reference personfuntionality. have assign implementer class object it.

interface

an interface contains signatures of methods, properties, events or indexers. class or struct implements interface must implement members of interface specified in interface definition. in following example, class implementationclass must implement method named samplemethod has no parameters , returns void.

interface isampleinterface {     void samplemethod(); }  class implementationclass : isampleinterface {     // explicit interface member implementation:       void isampleinterface.samplemethod()     {         // method implementation.     }      static void main()     {         // declare interface instance.         isampleinterface obj = new implementationclass();          // call member.         obj.samplemethod();     } } 

Popular posts from this blog