L
Leif K-Brooks
Is it possible to create a class in C# which behaves like another
object's property? Here's an example of what I want to be able to do,
using made up syntax:
class PropertyObject
{
private int value = 0;
public static int operator get(PropertyObject p)
{
return p.value;
}
public static void operator set(PropertyObject p, int value)
{
p.value = value;
}
}
class Foo
{
public int X = new PropertyObject();
}
Right now I can do the same thing with an explicit property in Foo:
class Foo
{
private PropertyObject x = new PropertyObject();
public int X
{
get
{
return x.Get();
}
set
{
x.Set(value);
}
}
}
But doing it that way leads to very verbose code, especially if the
property is used many times. Is there a better way?
object's property? Here's an example of what I want to be able to do,
using made up syntax:
class PropertyObject
{
private int value = 0;
public static int operator get(PropertyObject p)
{
return p.value;
}
public static void operator set(PropertyObject p, int value)
{
p.value = value;
}
}
class Foo
{
public int X = new PropertyObject();
}
Right now I can do the same thing with an explicit property in Foo:
class Foo
{
private PropertyObject x = new PropertyObject();
public int X
{
get
{
return x.Get();
}
set
{
x.Set(value);
}
}
}
But doing it that way leads to very verbose code, especially if the
property is used many times. Is there a better way?