One field, multiple properties

A

Anders Eriksson

I have a field
private double x;

Now I want to have two properties for this field. One that returns the
field as a double and one that returns the field as a string.

In my naivety I tried this

public double X
{
get { return x; }
set { x = value; }
}

public string X
{
get { return x.ToString(); }
set { x = double.Parse(value); }
}

But it fails since you obviously can't have proberties that only differ
on the return type.

So how do I do this?

// Anders
 
J

Jason Keats

Anders said:
I have a field
private double x;

Now I want to have two properties for this field. One that returns the
field as a double and one that returns the field as a string.

In my naivety I tried this

public double X
{
get { return x; }
set { x = value; }
}

public string X
{
get { return x.ToString(); }
set { x = double.Parse(value); }
}

But it fails since you obviously can't have proberties that only differ
on the return type.

So how do I do this?

Simple. Create properties with different names! ;-)

I've seen/used libraries that prefixed properties with s_ for string
versions.

double _x;

public double X
{
get { return _x; }
set { _x = value; }
}

public string s_X
{
get { return _x.ToString(); }
set { _x = double.Parse(value); }
}
 
A

Arne Vajhøj

I have a field
private double x;

Now I want to have two properties for this field. One that returns the
field as a double and one that returns the field as a string.

In my naivety I tried this

public double X
{
get { return x; }
set { x = value; }
}

public string X
{
get { return x.ToString(); }
set { x = double.Parse(value); }
}

But it fails since you obviously can't have proberties that only differ
on the return type.

So how do I do this?

Fundamentally you decide to use a type safe language so
live with that and do not try to make it not type safe.

If you want to proceed then see below for 4 different
approaches.

Arne

====

using System;

namespace E
{
public class WishyWashyData
{
// two properties
// misuse of properties
private int v1;
public int V1AsInt
{
get { return v1; }
set { v1 = value; }
}
public string V1AsString
{
get { return v1.ToString(); }
set { v1 = int.Parse(value); }
}
// four methods
// not the C# way
private int v2;
public int GetV2AsInt()
{
return v2;
}
public string GetV2AsString()
{
return v2.ToString();
}
public void SetV2(int v)
{
v2 = v;
}
public void SetV2(string v)
{
v2 = int.Parse(v);
}
// two generic methods
// overhead + not very typesafe
private int v3;
public T GetV3<T>()
{
return (T)Convert.ChangeType(v3, typeof(T));
}
public void SetV3<T>(T v)
{
v3 = (int)Convert.ChangeType(v, typeof(int));
}
// intermediate type with implicit conversions
// lot of code + cast necessary in some cases where C# can not
deduce conversion correct
private int v4;
public class IntOrString
{
private int real;
private IntOrString(int real)
{
this.real = real;
}
private IntOrString(string real)
{
this.real = int.Parse(real);
}
public static implicit operator int(IntOrString v)
{
return v.real;
}
public static implicit operator string(IntOrString v)
{
return v.real.ToString();
}
public static implicit operator IntOrString(int v)
{
return new IntOrString(v);
}
public static implicit operator IntOrString(string v)
{
return new IntOrString(v);
}
}
public IntOrString V4
{
get { return v4; }
set { v4 = value; }
}
}
public class Program
{
public static void Main(string[] args)
{
WishyWashyData o = new WishyWashyData();
o.V1AsString = "123";
Console.WriteLine(o.V1AsInt + 0);
o.V1AsInt = 456;
Console.WriteLine(o.V1AsString + 0);
o.SetV2("123");
Console.WriteLine(o.GetV2AsInt() + 0);
o.SetV2(456);
Console.WriteLine(o.GetV2AsString() + 0);
o.SetV3("123");
Console.WriteLine(o.GetV3<int>() + 0);
o.SetV3(456);
Console.WriteLine(o.GetV3<string>() + 0);
o.V4 = "123";
Console.WriteLine(o.V4 + 0);
o.V4 = 456;
Console.WriteLine((string)o.V4 + 0);
Console.ReadKey();
}
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top