mammoo

I'm writing an input handler class and I'm writing a function to return true or false depending on if the requested button is down or not for the supplied player. At the moment I'm doing it like this:

<code>
switch (MyRequestButton)
{
case "A":
if (currentState[player].Buttons.A == ButtonState.Pressed)
{
buttonDown = true;
}
break;

case "B":
if (currentState[player].Buttons.B == ButtonState.Pressed)
{
buttonDown = true;
}
break;
</code>

But what I'd like to do is something like this:

<code>
if (currentState[player].Buttons.MyRequestButton == ButtonState.Pressed)
{
buttonDown = true;
}
</code>

Can you see what I'm trying to do Instead of switching on the requested button, I just want to access that button directly using the supplied string with the button name. Is it possible to do this


Re: XNA Game Studio Express Can you access member variables dynamically?

Hlubocky

I think what you would typically do is create an enum with all of the buttons that can be pressed and then create an array that has length equal to the number of elements in the enum to store your buttons. Finally, you would cast the enum that is passed in to the function (which button to track) as an int and use it to index the array.




Re: XNA Game Studio Express Can you access member variables dynamically?

Shawn Hargreaves - MSFT

You can use the System.Reflection namespace (specifically the members of the Type class) to look up or modify the value of fields and properties dynamically by name. That's pretty slow compared to regular C# code, but might be ok if you're just doing it once per frame in your input reader.





Re: XNA Game Studio Express Can you access member variables dynamically?

mammoo

So basically, I can't do what it is I need to do Coming from web development experience I'm used to being able to do something like:

<code>
buttonValue = Eval(\"currentState.Buttons.\" + requestedButton);

or

buttonValue = currentState.Buttons[requestedButton];
</code>

Is there really nothing like this you can do in C#. How would other people avoid using massive switch statements






Re: XNA Game Studio Express Can you access member variables dynamically?

mammoo

Oops, that post didn't come out very well. Should have looked like:

So basically, I can't do what it is I need to do Coming from web development experience I'm used to being able to do something like:

<code>
buttonValue = Eval("currentState.Buttons." + requestedButton);

or

buttonValue = currentState.Buttons[requestedButton];
</code>

Is there really nothing like this you can do in C#. How would other people avoid using massive switch statements




Re: XNA Game Studio Express Can you access member variables dynamically?

Joel Martinez

Yes, there are ways to do what you are looking to do ... Shawn suggested one of them (reflection). The problem is that, unlike Javascript, C# is pre-compiled. It's not a big deal in javascript because it's all parsed and interpreted at runtime.

If you're dead set on using dynamic statement evaluation, use this LateBinder class I wrote ... it's very easy to use, and a little better than looking up the class' reflection information every time you need it.
http://codecube.net/item.asp cc_ItemID=307