Simple question, hopefully a simple answer:
I'd like to do the following:
private DateTime m_internalDateTime; public var DateTimeProperty { get { return m_internalDateTime.ToString(); } // Return a string set { m_internalDateTime = value; } // here value is of type DateTime }
The above is just an example of what I'm trying to do. I'd like to have a public accessor to an internal variable of type x. I want the get that variable as a string, but set it using something of type x.
Is this possible?
--edit--
I just realized I could do something like:
private DateTime m_internalDateTime; public object DateTimeProperty { get { return m_internalDateTime.ToString(); } // Return a string set { m_internalDateTime = (DateTime)value; } // here value is of type DateTime }
But then, let say I use type y instead of a "string" as my 'get' type. If I want to use "DateTimeProperty" else where in my code, I'd have to cast it.
In properties, a get accessor is used to return a property value and a set accessor is used to assign a new value. The value keyword in set accessor is used to define a value that is going to be assigned by the set accessor. In c#, the properties are categorized as read-write, read-only, or write-only.
Getters and setters are used to protect your data, particularly when creating classes. For each instance variable, a getter method returns its value while a setter method sets or updates its value. Given this, getters and setters are also known as accessors and mutators, respectively.
Getters (a.k.a. Accessors) simply return the title , author , and rating instance variables, respectively. Note that the name of the first getter is getTitle . Prefixing the corresponding instance variable with "get" is a common naming convention for getters. Getter methods shine in complex classes.
It is a good practice to use the same name for both the property and the private field, but with an uppercase first letter. The get method returns the value of the variable name . The set method assigns a value to the name variable. The value keyword represents the value we assign to the property.
No. You can obviously add the .ToString() in the calling code, but you can't do what you propose without different names like this:
private DateTime m_internalDateTime; public DateTime SetDateTime { set { m_internalDateTime = value; } } public string GetDateTime { get { return m_internalDateTime.ToString(); } }
Or, even better to use methods instead of properties (as noted in the comments):
private DateTime m_internalDateTime; public void SetDateTime(DateTime dateTime) { m_internalDateTime = dateTime; } public string GetDateTime() { return m_internalDateTime.ToString(); }
Keep in mind that var
is for implicitly, compile-time typed variables, not dynamic variables.
Definitely do not do what you noted in your edit. It introduced a break in convention, possible performance implications (albeit slight), and significant localization problems.
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