The online racing simulator
#1 - vane
Can i use 'foreach' to avoid having to put multiple 'if' *update new question*
name explains most of it, basically i am using this code:

private void timer2_Tick(object sender, EventArgs e)
{
Poll();

byte[] joystickbtn = state.GetButtons();
buttons = new bool[joystickbtn.Length];

int j = 0;
foreach (byte button in joystickbtn)
{
buttons[j] = button >= 128;
j++;
}
if (buttons[16])
{
label1.Text = ("16");
timer2.Enabled = false;
}

if (buttons[17])
{
label1.Text = ("17");
timer2.Enabled = false;
}

if (buttons[18])
{
label1.Text = ("18");
timer2.Enabled = false;
}

if (buttons[19])
{
label1.Text = ("19");
timer2.Enabled = false;
}

}

It detects button presses from a steering wheel, this is the button select feature from my game controller interface program.

Basically i want to know if i can use 'foreach' to make it so i only have to have one if statement.

i have tried:
if (buttons[j])
{
label1.Text = (j);
timer2.Enabled = false;
}

but it seems to not work, i would be greatful for any help available!

thanks
Peter
Well you don't say why it didn't work, but couldn't you do something like:

for (int i = 16; i <= 19; i++)
{
if (buttons[i])
{
label1.Text = i.ToString();
timer2.Enabled = false;
}
}

#3 - vane
Ha i knew it would be something silly thank you so much!

Haven't done much coding for a while and forgot some of it.
#4 - vane
I tried copying and pasting that bit of code to be put underneath another 'private void' thing, activated by a different timer and button but when i try and debug it comes up with an error saying 'Object reference not set to an instance of an object.' under the 'buttons = new bool[joystickbtn.Length]; line
It's very hard to know what's wrong without seeing the code.

The error you are getting is a NullReferenceException. It would appear that the "joystickbtn" variable has been declared but never assigned anything, so its value is null.
#6 - vane
well it does have ' byte[] joystickbtn = state.GetButtons();' right above it :s
#7 - vane
Ok this is weird, i tried using the 'open without debugging' option and now it works :s very strange! now the debugging menu is working as well
#8 - Silox
Quote from vane :Ok this is weird, i tried using the 'open without debugging' option and now it works :s very strange! now the debugging menu is working as well

You always should test it with the debugger before you compile it...
#9 - vane
i don't mean actually compiling it, in the debug menu instead of debug, there is open without debugging, i opened that and after that it just started working miraculously!
You need to learn how to debug your programs. Compiling your program in "release" is not fixing errors. If the debugger forces an error on you, it does so for a reason. You need to understand what the actual error is, instead of just glossing over it as you are.

Anyway, debugging is covered here, in the official reference.

FGED GREDG RDFGDR GSFDG