Alex Rivera | Logout

Accessor with different set and get types?

Asked 2009-06-09T18:31:18.503
25

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.

Edit
Report

1 Answer

4

Not that way, but you can certainly have a second property that accesses the m_internalDateTime field.

public string DateTimeString
{
   get { return m_internalDateTime.ToString(); }
}
answered 2009-06-09T18:33:47.657

Your Answer