Dynamically Reference Member Variable

Please support our C# advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Sep 2009
Posts: 12
Reputation: Ishbir is an unknown quantity at this point 
Solved Threads: 0
Ishbir Ishbir is offline Offline
Newbie Poster

Dynamically Reference Member Variable

 
0
  #1
Nov 4th, 2009
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-
  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-

  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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 2,053
Reputation: ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of 
Solved Threads: 311
ddanbe's Avatar
ddanbe ddanbe is offline Offline
Postaholic
 
0
  #2
Nov 4th, 2009
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.
Today is a gift, that's why it is called "The Present".
Make love, no war. Cave ab homine unius libri.
Danny
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 2,721
Reputation: adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of 
Solved Threads: 501
Moderator
adatapost's Avatar
adatapost adatapost is offline Offline
Posting Maven
 
1
  #3
Nov 5th, 2009
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 452
Reputation: Ramesh S will become famous soon enough Ramesh S will become famous soon enough 
Solved Threads: 82
Ramesh S Ramesh S is offline Offline
Posting Pro in Training
 
2
  #4
Nov 5th, 2009
Originally Posted by Ishbir View Post
In PHP, the equivalent would be-

  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.
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 3,444
Reputation: sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of 
Solved Threads: 626
Sponsor
sknake's Avatar
sknake sknake is offline Offline
.NET Enthusiast
 
1
  #5
Nov 5th, 2009
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() :

  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:
  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.
Scott Knake
Custom Software Development
Apex Software, Inc.
Reply With Quote Quick reply to this message  
Reply

Message:




Views: 389 | Replies: 4
Thread Tools Search this Thread



Tag cloud for C#
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC