Discussion:
Property with different types
(too old to reply)
Sonnich Jensen
2017-08-29 15:29:13 UTC
Permalink
Hi

Can I create a property as a string as well as a DateTime?

public string aaa....
public DateTime aaa....

I want to write it as a DateTime, and read it as a string.

The best I can think of is a class where I overwrite ToString

Any better ideas?
Marcel Mueller
2017-08-29 20:01:24 UTC
Permalink
Post by Sonnich Jensen
Can I create a property as a string as well as a DateTime?
public string aaa....
public DateTime aaa....
No, this is not supported by the language.
Post by Sonnich Jensen
I want to write it as a DateTime, and read it as a string.
You need to define two distinct names for these two properties.
Post by Sonnich Jensen
The best I can think of is a class where I overwrite ToString
Any better ideas?
Rethink your requirement. This kind of design is evidence to suggest a
confusing or inconsistent software architecture.


Marcel
Arne Vajhøj
2017-08-30 00:36:02 UTC
Permalink
Post by Marcel Mueller
Post by Sonnich Jensen
Any better ideas?
Rethink your requirement. This kind of design is evidence to suggest a
confusing or inconsistent software architecture.
The ability to do some tricky stuff is one of the reasons for
having getters and setters in the first place.

Obviously not ideal, but sometimes things happens.

Arne
Registered User
2017-08-30 00:26:20 UTC
Permalink
On Tue, 29 Aug 2017 08:29:13 -0700 (PDT), Sonnich Jensen
Post by Sonnich Jensen
Hi
Can I create a property as a string as well as a DateTime?
public string aaa....
public DateTime aaa....
I want to write it as a DateTime, and read it as a string.
The best I can think of is a class where I overwrite ToString
Any better ideas?
Somewhere conversion between types will be necessary. Any override of the
ToString function many not be obvious to child types or consumers. An explicit
method such as

string ThisTypesDateToStringMethod() {
return ...; // convert DateTime property 'aaa' to desired string
}

will be more clear to consumers of the type and will not disguise the type's
assumed ToString method.. Explicit verbosity may cost more key strokes but the
preloading will simpify the type for both consumers and derived types.

In general make no assumptions about overridden class methods unless you're
willing to document how and why the override is better than what the base class
provides.

regards
A.G.
Arne Vajhøj
2017-08-30 00:33:54 UTC
Permalink
Post by Sonnich Jensen
Can I create a property as a string as well as a DateTime?
public string aaa....
public DateTime aaa....
I want to write it as a DateTime, and read it as a string.
The best I can think of is a class where I overwrite ToString
Any better ideas?
Well - you can obviously not have two properties with the same name.

I have two ideas.

1)

The pragmatic:

public class Foobar
{
private DateTime theTime;
public DateTime TheTimeAsDateTime
{
get { return theTime; }
set { theTime = value; }
}
public String TheTimeAsString
{
get { return theTime.ToString(); }
set { theTime = DateTime.Parse(value); }
}
}

It may not be so elegant. But it is very easy to understand. And
that should really be the main criteria. So this is what I would
do.

2)

The tricky:

public class DateTimeOrString
{
private DateTime dt;
private DateTimeOrString(DateTime dt)
{
this.dt = dt;
}
private DateTime Get()
{
return dt;
}
public static implicit operator DateTimeOrString(DateTime dt)
{
return new DateTimeOrString(dt);
}
public static implicit operator DateTimeOrString(String s)
{
return new DateTimeOrString(DateTime.Parse(s));
}
public static implicit operator DateTime(DateTimeOrString dts)
{
return dts.Get();
}
public static implicit operator String(DateTimeOrString dts)
{
return dts.Get().ToString();
}
}
public class Foobar
{
public DateTimeOrString TheTime { get; set; }
}

It may be elegant as it is almost invisible to the callers.
But how many C# developers are really familiar with implicit
operators. So risk of confusion and problems.

Arne
bradbury9
2017-09-07 09:42:39 UTC
Permalink
Post by Arne Vajhøj
Post by Sonnich Jensen
Can I create a property as a string as well as a DateTime?
public string aaa....
public DateTime aaa....
I want to write it as a DateTime, and read it as a string.
The best I can think of is a class where I overwrite ToString
Any better ideas?
Well - you can obviously not have two properties with the same name.
I have two ideas.
1)
public class Foobar
{
private DateTime theTime;
public DateTime TheTimeAsDateTime
{
get { return theTime; }
set { theTime = value; }
}
public String TheTimeAsString
{
get { return theTime.ToString(); }
set { theTime = DateTime.Parse(value); }
}
}
It may not be so elegant. But it is very easy to understand. And
that should really be the main criteria. So this is what I would
do.
I would suggest for that specific case modifing it a bit to avoid misusage:

private DateTime theTime;
public DateTime TheTimeAsDateTime
{
set { theTime = value; }
}
public String TheTimeAsString
{
get { return theTime.ToString(); }
}
Post by Arne Vajhøj
2)
public class DateTimeOrString
{
private DateTime dt;
private DateTimeOrString(DateTime dt)
{
this.dt = dt;
}
private DateTime Get()
{
return dt;
}
public static implicit operator DateTimeOrString(DateTime dt)
{
return new DateTimeOrString(dt);
}
public static implicit operator DateTimeOrString(String s)
{
return new DateTimeOrString(DateTime.Parse(s));
}
public static implicit operator DateTime(DateTimeOrString dts)
{
return dts.Get();
}
public static implicit operator String(DateTimeOrString dts)
{
return dts.Get().ToString();
}
}
public class Foobar
{
public DateTimeOrString TheTime { get; set; }
}
It may be elegant as it is almost invisible to the callers.
But how many C# developers are really familiar with implicit
operators. So risk of confusion and problems.
Arne
Arne Vajhøj
2017-09-07 21:17:47 UTC
Permalink
Post by Arne Vajhøj
Post by Arne Vajhøj
Post by Sonnich Jensen
Can I create a property as a string as well as a DateTime?
public string aaa....
public DateTime aaa....
I want to write it as a DateTime, and read it as a string.
The best I can think of is a class where I overwrite ToString
Any better ideas?
public class Foobar
{
private DateTime theTime;
public DateTime TheTimeAsDateTime
{
get { return theTime; }
set { theTime = value; }
}
public String TheTimeAsString
{
get { return theTime.ToString(); }
set { theTime = DateTime.Parse(value); }
}
}
private DateTime theTime;
public DateTime TheTimeAsDateTime
{
set { theTime = value; }
}
public String TheTimeAsString
{
get { return theTime.ToString(); }
}
That is certainly sufficient for OP's questions.

I added all 4 because I did not see a big problem with
the extra 2.

Arne

Loading...