Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessor with different set and get types?

Tags:

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.

like image 946
Nick Avatar asked Jun 09 '09 18:06

Nick


People also ask

What is a get or set accessor?

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.

Are getters and accessors the same?

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.

Is a getter an accessor?

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.

Why we use get and set with properties?

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.


1 Answers

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.

like image 81
Michael Haren Avatar answered Nov 08 '22 21:11

Michael Haren