properties - C# related on Constructor and Property -


i building tiny lib, , have run problem. want provide two-way solution, example:

how can accomplish this? getting exception thrown, because expects something... example welcomed :) thanks!

edit: executing something, code similar one:

 system.io.driveinfo d = new system.io.driveinfo("c:");  

i want achieve class following:

driver d = new driver();  d.driverletter = "c:";  

and still same results, use managementobjectsearch, managementobjectcollection , other system.management classes.

you need provide both constructors:

public class person {     public string name { get; set; }     public int age { get; set; }     public string country { get; set; }      // paramterless constructor  -    new person();     // properties default values (string="" , int=0)     public person () { }      // parameterized constructor -    new person("joe", 16, "usa");     public person (string name, int age, string country)     {         name = name;         age = age;         country = country;     } } 

if define parameterized constructor, default parameterless constructor not included you. therefore need include yourself.

from msdn 10.10.4 default constructors:

if class contains no instance constructor declarations, default instance constructor automatically provided.


Comments

Popular posts from this blog

Change php variable from jquery value using ajax (same page) -

Pull out data related to my apps from Android Play Store and iOS App Store -

How can I fetch data from a web server in an android application? -