Property objects?

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?
 
F

Frans Bouma [C# MVP]

Leif said:
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?

No. And believe me, your way isn't better. Let's explain a few things
about .NET and code/data at runtime.

If you have 10 classes, which each have 10 properties, you have 100
getters and 100 setters. These are methods in the objects at runtime
and take no memory, other than the code bytes. .NET shares the code of
a class among all instances of that class. However your approach would
require a new instance of the propertyobject for each property, in each
instance of the class, so say if each instance of those classes is in
memory you have 100 propertyobject instances. That's not efficient.

Code is cheap, do as much as you can in code, instead of in objects.

So write out the get/set clauses of properties in each class, it's
better.

FB



--
 
J

Joanna Carter [TeamB]

"Leif K-Brooks" <[email protected]> a écrit dans le message de [email protected]...

| 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:

Here is a basic version of the generic type that we use in our frameworks :

public class Property
{
...
}

public class Property<T> : Property
{
private T value;

public Property(T value)
{
this.value = value;
}

protected void SetValue(T value)
{
this.value = value;

OnPropertyChanged();
}

public T Value
{
get { return value; }
set { SetValue(value); }
}
}

this then get used like this :

public class BaseClass
{
private Dictionary<string, Property> properties = new Dictionary<string,
Property>();

protected T GetValue<T>(string name)
{
return ((Property<T>) properties[name]).Value;
}

protected virtual void SetValue<T>(string name, T value)
{
((Property<T>) properties[name]).Value = value;
}
}

public class Customer : BaseClass
{
public string Name
{
get { return GetValue<string>("Name"); }
set { SetValue<string>("Name", value); }
}
}

Less code would be a problem, in fact, more is required.

Joanna
 

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