getter setter - Auto setting properties in C# -
getter setter - Auto setting properties in C# -
i have class called place uses getters , setters so:
public class place { public string address { get; set; } public geocoordinate location { set { // geocode address field , receive geocoding response, initializing new instance of geocoordinate class - effort below.. igeocoder geocoder = new googlegeocoder(); ienumerable<address> matchedaddresses = geocoder.geocode(this.address); this.location = new geocoordinate(matchedaddresses.cast<address>().first().coordinates.latitude, matchedaddresses.cast<address>().first().coordinates.longitude); } { homecoming this.location; } } } and want create new instance of class place so:
place theplace = new place() { address = "123 false street, london" }; how automatically trigger setter location variable when address property set address fed in automatically geocoded , geocoordinate object set automatically?
you need alter address auto-property "regular" 1 (i.e. property composed variable , getter/setter pair), this:
private string address; public string address { {return address;} set { // prepare location geocoordinate loc = getgeocoordinatefromaddress(value); // store address future reference address = value; // setting location going through property triggers setter location = loc; } } c# getter-setter
Comments
Post a Comment