Let's say I create a class with a property:
public class User
{
private string _userID;
public string UserID
{
get { return _userID; }
set { _userID = value; }
}
}
What do I have to do with the class and property to be able to have a method attached to the UserID property, such as a method that generates Xml around the user ID with the "dot" syntax:
User u = new User();
u.UserID = "Mike";
string xml = u.UserID.ToXml();
I can figure out how to write a method to put Xml tags around the value of the UserID, the part that is eluding me is how to make the method work with the property using the "dot" syntax.
All of these answers are useful, and thanks to everyone for contributing. In fact, the answer I marked as "accepted" was precisely what I was looking for. I appreciate the cautionary remakrs on extension methods (which I had never heard of before this), and of course it could be a problem to apply the extension method to all strings in some circumstances, but in this case I definitely wanted to apply the method ToXml() to all string properties in the class. Just what the doctor ordered. I am quite familiar with XmlSerialization, but in this case needed to avoid it for various reasons.
In most cases, methods are actions and properties are qualities. Using a method causes something to happen to an object, while using a property returns information about the object or causes a quality about the object to change.
Properties. C# properties are class members that expose functionality of methods using the syntax of fields. They simplify the syntax of calling traditional get and set methods (a.k.a. accessor methods). Like methods, they can be static or instance.
Calling a method is like accessing a field. After the object name (if you're calling an instance method) or the type name (if you're calling a static method), add a period, the name of the method, and parentheses. Arguments are listed within the parentheses and are separated by commas.
Properties overview Properties enable a class to expose a public way of getting and setting values, while hiding implementation or verification code. A get property accessor is used to return the property value, and a set property accessor is used to assign a new value.
There are two ways:
Either the object/value that you're operating on has to have that method (such as when you have control over the object definition.)
public class UserIDObject
{
public string ToXML() {}
}
public class User
{
private UserIDObject _userID;
public UserIDObject UserID
{
get { return _userID; }
set { _userID = value; }
}
}
User u = new User();
u.UserID = = new UserIDObject("Mike");
string xml = u.UserID.ToXml();
Or you can implement an Extension Method (MSDN) for the object. Geeks with blogs has a good tutorial on Extension Methods and they can be applied to .NET 2.0 if you're running VS2008.
public static class MyMethods
{
public static string ToXML(this string s) { }
}
Extension Methods are something that you want to weight heavily since an extension method applies to ALL objects of that class type. (IE. do I want all strings to be able to call .ToXML()?)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With