944,124 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Unsolved
  • Views: 1734
  • C# RSS
Nov 4th, 2009
0

Dynamically Reference Member Variable

Expand Post »
I have a function which initiates commands for a command class. Since there are many commands to initiate, writing code for each one would be tedious.

The function definition is as follows-
C# Syntax (Toggle Plain Text)
  1. private static void InitCommand(KeyGesture input, String text, String name)
  2. {
  3. InputGestureCollection inputs = new InputGestureCollection();
  4. inputs.Add(input);
  5.  
  6. Type t = typeof(UploadCommands);
  7. t.GetMember(name);
  8.  
  9. (RoutedUICommand)t.GetMember(name) = new RoutedUICommand(text, name, typeof(UploadCommands), inputs);
  10. }

Now, my problem is that I wish to reference the commands dynamically. I pass in the command name, it references the public static RoutedUICommand variable.

In PHP, the equivalent would be-

PHP Syntax (Toggle Plain Text)
  1. $class->$member = "Blah, blah";

I can't figure out how this would be done in C#.
Last edited by Ishbir; Nov 4th, 2009 at 11:04 am.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Ishbir is offline Offline
12 posts
since Sep 2009
Nov 4th, 2009
0
Re: Dynamically Reference Member Variable
Are you referring to a class field in C#?
Then use property syntax to access it.
Your code looks a bit strange, why are you making a inputs collection and just put one item input into it?
Last edited by ddanbe; Nov 4th, 2009 at 12:02 pm.
Reputation Points: 2035
Solved Threads: 645
Senior Poster
ddanbe is offline Offline
3,740 posts
since Oct 2008
Nov 5th, 2009
1
Re: Dynamically Reference Member Variable
Moderator
Reputation Points: 2136
Solved Threads: 1228
Posting Genius
adatapost is offline Offline
6,527 posts
since Oct 2008
Nov 5th, 2009
2
Re: Dynamically Reference Member Variable
Click to Expand / Collapse  Quote originally posted by Ishbir ...
In PHP, the equivalent would be-

PHP Syntax (Toggle Plain Text)
  1. $class->$member = "Blah, blah";

I can't figure out how this would be done in C#.
Hi Ishbir,

.NET have this feature. It is called Reflection in .NET.

You can use reflection to dynamically create an instance of a type(class), bind the type to an existing object, or get the type from an existing object and invoke its methods or access its fields and properties.

Refer this link and also the link given by adatapost.
Reputation Points: 165
Solved Threads: 113
Posting Pro
Ramesh S is offline Offline
580 posts
since Jun 2009
Nov 5th, 2009
1
Re: Dynamically Reference Member Variable
You may also consider using an enumeration member to define all of your commands. This way you have a strongly typed way to reference the command. You can call .ToString() on the enumeration member and call Enum.Parse() :

C# Syntax (Toggle Plain Text)
  1. internal enum CommandTypes
  2. {
  3. Command1 = 1,
  4. Command2 = 2
  5. }
  6. private void button2_Click(object sender, EventArgs e)
  7. {
  8. CommandTypes cmd = CommandTypes.Command2;
  9. string sCmdString = cmd.ToString();
  10. Console.WriteLine("cmd string value: " + sCmdString);
  11. Console.WriteLine("cmd int value: " + ((int)cmd).ToString());
  12. CommandTypes parsedCmd = (CommandTypes)Enum.Parse(typeof(CommandTypes), sCmdString);
  13. System.Diagnostics.Debugger.Break();
  14. }

Results in:
text Syntax (Toggle Plain Text)
  1. cmd string value: Command2
  2. cmd int value: 2

You could then go on to design an attribute and use the enumeration member in your attribute for the class member that represents the command.
Featured Poster
Reputation Points: 1749
Solved Threads: 735
Senior Poster
sknake is offline Offline
3,948 posts
since Feb 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: Saving and Discarding mails
Next Thread in C# Forum Timeline: UDP Port Forwarding





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC