473,465 Members | 1,651 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Passing Properties to Generics

I am trying to create a generic Command object (following the Command design
pattern) that allows me to specify a generic type, an object to operate on,
and most importantly, a public Property on that object for getting and
setting the value in question.

Currently the only approach I've found to work is to use delegates along
with Get and Set functions (instead of using a Property). See code below.

Anyone have a better approach than what I have as follows?
class Person
{
private string name = "";
private int age = 0;

public string Name { set { name = value; } get { return name; } }
public int Age { set { age = value; } get { return age; } }

// These needed to be added to get the Generic PersonCommand to work
public void SetName(string Name) { name = Name; }
public string GetName() { return name; }
}
abstract class Command
{
public abstract void Do();
public abstract void Undo();
}
class PersonCommand<T> : Command
{
private Person person = null;
private T oldValue;
private T newValue;
private SetPersonValue setPersonValue;
private GetPersonValue getPersonValue;

// These are needed because I can't work with Properties
public delegate void SetPersonValue(T value);
public delegate T GetPersonValue();

public PersonCommand(Person P, SetPersonValue setValue,
GetPersonValue getValue,
T NewValue)
{
setPersonValue = setValue;
getPersonValue = getValue;

oldValue = getPersonValue();
newValue = NewValue;
person = P;
}

public override void Do()
{
setPersonValue(newValue);
// Would prefer to do the following:
// person.??? = newValue;
}

public override void Undo()
{
setPersonValue(oldValue);
}
}
public void test()
{
Person Robert = new Person();
Robert.Name = "Robert";
Robert.Age = 30;

PersonCommand<string> name2Command = new
PersonCommand<string>(Robert, new
PersonCommand<string>.SetPersonValue(Robert.SetNam e),
new PersonCommand<string>.GetPersonValue(Robert.GetNam e),
"Bobby");

name2Command.Do();
Debug.WriteLine("Name = " + Robert.Name);
name2Command.Undo();
Debug.WriteLine("Name = " + Robert.Name);

}

Nov 17 '05 #1
2 3546
Jeff,
Have you considered using a PropertyDescriptor instead of a Delegate?

http://msdn.microsoft.com/library/de...classtopic.asp

Your Command object would accept the type of the Component (Person), the
Type of Property and the name of the Property, it could then use
TypeDescriptro.GetProperties to lookup the PropertyDescriptor for that
field. I would consider making the first type type parameters, while the
third would be a parameter to the constructor.

http://msdn.microsoft.com/library/de...rtiesTopic.asp

Then Get & Set would then use PropertyDescriptor.GetValue &
PropertyDescriptor.SetValue to change the values...

Something like (untested, not syntax checked):

class GenericCommand<TComponent, TProperty> : Command
{
private TComponent person = null;
private PropertyDescriptor descriptor = null;
private TProperty oldValue;
private TProperty newValue;

public GenericCommand(TComponent P, String property,
TProperty NewValue)
{
descriptor =
TypeDescriptor.GetProperties(typeof(TComponent))[property];
person = P;
oldValue = Value;
newValue = NewValue;
}

public TProperty Value
{
get
{
return (TProperty)descriptor.GetValue(person);
{
set
{
descriptor.SetValue(person, value);
}
}

public override void Do()
{
Value = newValue;
}

public override void Undo()
{
Value = oldValue;
}
}

Even without the other stuff, you could base the Value Property on your
GetPersonValue & SetPersonValue delegates...

Hope this helps
Jay
"Jeff Bush" <je******@nospam.com> wrote in message
news:8D**********************************@microsof t.com...
|I am trying to create a generic Command object (following the Command
design
| pattern) that allows me to specify a generic type, an object to operate
on,
| and most importantly, a public Property on that object for getting and
| setting the value in question.
|
| Currently the only approach I've found to work is to use delegates along
| with Get and Set functions (instead of using a Property). See code below.
|
| Anyone have a better approach than what I have as follows?
|
|
| class Person
| {
| private string name = "";
| private int age = 0;
|
| public string Name { set { name = value; } get { return name; } }
| public int Age { set { age = value; } get { return age; } }
|
| // These needed to be added to get the Generic PersonCommand to
work
| public void SetName(string Name) { name = Name; }
| public string GetName() { return name; }
| }
|
|
| abstract class Command
| {
| public abstract void Do();
| public abstract void Undo();
| }
|
|
| class PersonCommand<T> : Command
| {
| private Person person = null;
| private T oldValue;
| private T newValue;
| private SetPersonValue setPersonValue;
| private GetPersonValue getPersonValue;
|
| // These are needed because I can't work with Properties
| public delegate void SetPersonValue(T value);
| public delegate T GetPersonValue();
|
| public PersonCommand(Person P, SetPersonValue setValue,
| GetPersonValue getValue,
| T NewValue)
| {
| setPersonValue = setValue;
| getPersonValue = getValue;
|
| oldValue = getPersonValue();
| newValue = NewValue;
| person = P;
| }
|
| public override void Do()
| {
| setPersonValue(newValue);
| // Would prefer to do the following:
| // person.??? = newValue;
| }
|
| public override void Undo()
| {
| setPersonValue(oldValue);
| }
| }
|
|
| public void test()
| {
| Person Robert = new Person();
| Robert.Name = "Robert";
| Robert.Age = 30;
|
| PersonCommand<string> name2Command = new
| PersonCommand<string>(Robert, new
| PersonCommand<string>.SetPersonValue(Robert.SetNam e),
| new PersonCommand<string>.GetPersonValue(Robert.GetNam e),
| "Bobby");
|
| name2Command.Do();
| Debug.WriteLine("Name = " + Robert.Name);
| name2Command.Undo();
| Debug.WriteLine("Name = " + Robert.Name);
|
| }
|
|
|
Nov 17 '05 #2
Hmm...

| field. I would consider making the first type type parameters, while the
That should be "the first two type parameters, while the"...

Also looking at it just now; the TComponent type parameter is not
specifically needed (GetProperties can accept either an object or a type),
although TComponent does bind GenericCommand to Person which could be
nice...

Hope this helps
Jay

"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:OZ**************@TK2MSFTNGP14.phx.gbl...
| Jeff,
| Have you considered using a PropertyDescriptor instead of a Delegate?
|
|
http://msdn.microsoft.com/library/de...classtopic.asp
|
| Your Command object would accept the type of the Component (Person), the
| Type of Property and the name of the Property, it could then use
| TypeDescriptro.GetProperties to lookup the PropertyDescriptor for that
| field. I would consider making the first type type parameters, while the
| third would be a parameter to the constructor.
|
|
http://msdn.microsoft.com/library/de...rtiesTopic.asp
|
| Then Get & Set would then use PropertyDescriptor.GetValue &
| PropertyDescriptor.SetValue to change the values...
|
| Something like (untested, not syntax checked):
|
| class GenericCommand<TComponent, TProperty> : Command
| {
| private TComponent person = null;
| private PropertyDescriptor descriptor = null;
| private TProperty oldValue;
| private TProperty newValue;
|
| public GenericCommand(TComponent P, String property,
| TProperty NewValue)
| {
| descriptor =
| TypeDescriptor.GetProperties(typeof(TComponent))[property];
| person = P;
| oldValue = Value;
| newValue = NewValue;
| }
|
| public TProperty Value
| {
| get
| {
| return (TProperty)descriptor.GetValue(person);
| {
| set
| {
| descriptor.SetValue(person, value);
| }
| }
|
| public override void Do()
| {
| Value = newValue;
| }
|
| public override void Undo()
| {
| Value = oldValue;
| }
| }
|
| Even without the other stuff, you could base the Value Property on your
| GetPersonValue & SetPersonValue delegates...
|
| Hope this helps
| Jay
|
|
| "Jeff Bush" <je******@nospam.com> wrote in message
| news:8D**********************************@microsof t.com...
||I am trying to create a generic Command object (following the Command
| design
|| pattern) that allows me to specify a generic type, an object to operate
| on,
|| and most importantly, a public Property on that object for getting and
|| setting the value in question.
||
|| Currently the only approach I've found to work is to use delegates along
|| with Get and Set functions (instead of using a Property). See code
below.
||
|| Anyone have a better approach than what I have as follows?
||
||
|| class Person
|| {
|| private string name = "";
|| private int age = 0;
||
|| public string Name { set { name = value; } get { return name; } }
|| public int Age { set { age = value; } get { return age; } }
||
|| // These needed to be added to get the Generic PersonCommand to
| work
|| public void SetName(string Name) { name = Name; }
|| public string GetName() { return name; }
|| }
||
||
|| abstract class Command
|| {
|| public abstract void Do();
|| public abstract void Undo();
|| }
||
||
|| class PersonCommand<T> : Command
|| {
|| private Person person = null;
|| private T oldValue;
|| private T newValue;
|| private SetPersonValue setPersonValue;
|| private GetPersonValue getPersonValue;
||
|| // These are needed because I can't work with Properties
|| public delegate void SetPersonValue(T value);
|| public delegate T GetPersonValue();
||
|| public PersonCommand(Person P, SetPersonValue setValue,
|| GetPersonValue getValue,
|| T NewValue)
|| {
|| setPersonValue = setValue;
|| getPersonValue = getValue;
||
|| oldValue = getPersonValue();
|| newValue = NewValue;
|| person = P;
|| }
||
|| public override void Do()
|| {
|| setPersonValue(newValue);
|| // Would prefer to do the following:
|| // person.??? = newValue;
|| }
||
|| public override void Undo()
|| {
|| setPersonValue(oldValue);
|| }
|| }
||
||
|| public void test()
|| {
|| Person Robert = new Person();
|| Robert.Name = "Robert";
|| Robert.Age = 30;
||
|| PersonCommand<string> name2Command = new
|| PersonCommand<string>(Robert, new
|| PersonCommand<string>.SetPersonValue(Robert.SetNam e),
|| new PersonCommand<string>.GetPersonValue(Robert.GetNam e),
|| "Bobby");
||
|| name2Command.Do();
|| Debug.WriteLine("Name = " + Robert.Name);
|| name2Command.Undo();
|| Debug.WriteLine("Name = " + Robert.Name);
||
|| }
||
||
||
|
|
Nov 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: domeceo | last post by:
can anyone tell me why I cannot pass values in a setTimeout function whenever I use this function it says "menu is undefined" after th alert. function imgOff(menu, num) { if (document.images) {...
3
by: James Allen Bressem | last post by:
If you aare going to change the entire Basic language in a new distribution the least Microsoft coulds have done for VBers is give us a new simplified syntax. I have seen many cases in VB.net where...
6
by: MSDNAndi | last post by:
Hi, I have a baseclass (non-static) with some static and some non-static methods/fields/properties. In the baseclass in one of the static methods I need to do something like " somelogic...
7
by: Tim | last post by:
When there is a need to pass some dynamic information between 2 managed assemblies, the "Dictionary object" in Generic form can be used as a method parameter to pass the information. The...
7
by: Rene | last post by:
We all know that we can't call custom methods or properties form generic type parameters (<T>) by default because the compiler will complain about his. For example, the following won't normally...
3
by: cwertman | last post by:
I have a question regarding dynamic properties. I have an Object say Account --Id --Prefix --Fname --Lname --Suffix
3
by: SteveT | last post by:
I'm currently creating a custom column in a DataGridView. I'm wondering if Generics can be used to solve an issue I'm seeing. When creating the editing control for the column cell I have the...
9
by: Chris | last post by:
Ok, so I have this sub I wrote, and I create a new instance of a UserControl: ctrlAPs tempctrl = new ctrlAPs(); Now, I would like to be able to use this sub I wrote for more than one...
8
by: MMAS | last post by:
Hey everyone -- Curious about some strange behaviour I'm seeing that seems to be related to my lack of understanding on how generics work in C#. Here's some simplified code (sorry for strange...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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,...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.