473,397 Members | 2,099 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 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 1564
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: sneill | last post by:
How is it possible to take the value of a variable (in this case, MODE_CREATE, MODE_UPDATE, etc) and use that as an object property name? In the following example I want 'oIcon' object to have...
3
by: Erik Harris | last post by:
I apologize if this is a stupid question - I'm relatively new to OOP. I have a property that must exist in a class in order to be used by another class. The property, however, does not change with...
15
by: Sam Kong | last post by:
Hello! I got recently intrigued with JavaScript's prototype-based object-orientation. However, I still don't understand the mechanism clearly. What's the difference between the following...
1
by: David Veeneman | last post by:
This posting is for the Google crawler and requires no response. I have created a multi-level object hierarchy, in which the root object needs to be notified whenever any property of any object...
2
by: garyusenet | last post by:
I could do with something similiar, can you tell me if you think this would work for me, and if there's any advantage in working with controls this way than how I currently am. At the moment...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.