471,605 Members | 1,613 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,605 software developers and data experts.

Property objects?

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?
Feb 7 '06 #1
2 1477
Leif K-Brooks wrote:
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

--
------------------------------------------------------------------------
Get LLBLGen Pro, productive O/R mapping for .NET: http://www.llblgen.com
My .NET blog: http://weblogs.asp.net/fbouma
Microsoft MVP (C#)
------------------------------------------------------------------------
Feb 7 '06 #2
"Leif K-Brooks" <eu*****@ecritters.biz> a écrit dans le message de news:
43***********************@news.sover.net...

| 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

--
Joanna Carter [TeamB]
Consultant Software Engineer
Feb 7 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by David Veeneman | last post: by
reply views Thread by leo001 | last post: by
reply views Thread by MichaelMortimer | last post: by
reply views Thread by CCCYYYY | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.