473,890 Members | 1,505 Online
Bytes | Software Development & Data Engineering Community
+ 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(P erson 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<s tring> name2Command = new
PersonCommand<s tring>(Robert, new
PersonCommand<s tring>.SetPerso nValue(Robert.S etName),
new PersonCommand<s tring>.GetPerso nValue(Robert.G etName),
"Bobby");

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

}

Nov 17 '05 #1
2 3562
Jeff,
Have you considered using a PropertyDescrip tor 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 PropertyDescrip tor 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 PropertyDescrip tor.GetValue &
PropertyDescrip tor.SetValue to change the values...

Something like (untested, not syntax checked):

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

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

public TProperty Value
{
get
{
return (TProperty)desc riptor.GetValue (person);
{
set
{
descriptor.SetV alue(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******@nospa m.com> wrote in message
news:8D******** *************** ***********@mic rosoft.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(P erson 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<s tring> name2Command = new
| PersonCommand<s tring>(Robert, new
| PersonCommand<s tring>.SetPerso nValue(Robert.S etName),
| new PersonCommand<s tring>.GetPerso nValue(Robert.G etName),
| "Bobby");
|
| name2Command.Do ();
| Debug.WriteLine ("Name = " + Robert.Name);
| name2Command.Un do();
| 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******** ******@TK2MSFTN GP14.phx.gbl...
| Jeff,
| Have you considered using a PropertyDescrip tor 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 PropertyDescrip tor 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 PropertyDescrip tor.GetValue &
| PropertyDescrip tor.SetValue to change the values...
|
| Something like (untested, not syntax checked):
|
| class GenericCommand< TComponent, TProperty> : Command
| {
| private TComponent person = null;
| private PropertyDescrip tor descriptor = null;
| private TProperty oldValue;
| private TProperty newValue;
|
| public GenericCommand( TComponent P, String property,
| TProperty NewValue)
| {
| descriptor =
| TypeDescriptor. GetProperties(t ypeof(TComponen t))[property];
| person = P;
| oldValue = Value;
| newValue = NewValue;
| }
|
| public TProperty Value
| {
| get
| {
| return (TProperty)desc riptor.GetValue (person);
| {
| set
| {
| descriptor.SetV alue(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******@nospa m.com> wrote in message
| news:8D******** *************** ***********@mic rosoft.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(P erson 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<s tring> name2Command = new
|| PersonCommand<s tring>(Robert, new
|| PersonCommand<s tring>.SetPerso nValue(Robert.S etName),
|| new PersonCommand<s tring>.GetPerso nValue(Robert.G etName),
|| "Bobby");
||
|| name2Command.Do ();
|| Debug.WriteLine ("Name = " + Robert.Name);
|| name2Command.Un do();
|| 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
14972
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) { document.images.src = eval("mt" +menu+ ".src") } alert("imgOff_hidemenu"); hideMenu=setTimeout('Hide(menu,num)',500);
3
1405
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 they seem to have had "no insightfullness" into these considerations. Their implementation of the property for example is gaudy at best: Property Name(ByVal Value As String) As String Get
6
1787
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 .... CallToStaticMethod();
7
10241
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 information that needs to be passed can be stored as Key-Value pairs, and the method signature remains the same. That way, handling future requirements of passing additional details to the callee can be handled without changing the method signature. Is...
7
1341
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 compile: T.MyCustomMethod() The compiler will tell me that it's not sure that the generic type parameters (<T>) will contain such method and will requires me to constrain the generic type parameters.
3
3358
by: cwertman | last post by:
I have a question regarding dynamic properties. I have an Object say Account --Id --Prefix --Fname --Lname --Suffix
3
1226
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 code: class MyEditingControl : MyTextBox, IDataGridViewEditingControl { ... } (The control is currently working just fine.) The column class is defined like this: class MySpecialColumn : DataGridViewColumn { ... }
9
1573
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 UserControl, so I was trying to do something like this: private void somesub(UserControl sourcectrl) {
8
3157
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 formatting) to show what my issue is. I have a generic abstract class here: public abstract class MyGenericAbstractClass<Twhere T : MyInterface
0
9978
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9822
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10923
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
8016
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7169
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5851
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
6045
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4676
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 we have to send another system
3
3277
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.