¿ªÔÆÌåÓý

ctrl + shift + ? for shortcuts
© 2025 Groups.io

EventArgs with For/Loop


 

The following doesn't allow the For loop to increment int "i" beyond 0 when args are within the loop. If I comment out the args statements then I see 0-15 values from the For loop in text console.?
Any thoughts?

public void ClearAllButtonFB()
? ? ? ? {
? ? ? ? ? ? MyEventArgs args = new MyEventArgs();
? ? ? ? ? ? {
? ? ? ? ? ? ? ? for (int i = 0; i < _buttons.Length; i++)
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? _buttons[i].BUTTON_FB = 0;
? ? ? ? ? ? ? ? ? ? args.MyBUTTONINDEX = (ushort)(i);
? ? ? ? ? ? ? ? ? ? args.MyBUTTON_FB = _buttons[i].BUTTON_FB;
? ? ? ? ? ? ? ? ? ? CrestronConsole.PrintLine("ClearAllButtons {0} Feedback Status is {1}\n", i, args.MyBUTTON_FB);
? ? ? ? ? ? ? ? ? ? MyEventToCall?.Invoke(this, args);
? ? ? ? ? ? ? ? }
?
? ? ? ? ? ? }
? ? ? ? }


 

Are you SURE that your _buttons array actually has actual objects in it?

declaring an array of a certain length, is just a container for "possible" object references - it doesn't mean there are objects there.

i.e.
Button[] _buttons = new Button[16];
is, at that point, an array of 16 null references to potential concrete Button instances.

i.e. It's quite possible that
_buttons[0] = A real button object
_buttons[1] = null

... and in that case _buttons[1].BUTTON_FB will throw a null reference exception, and you won't get to the PrintLine (or anything else!)

I think what you're doing is a great way to get stuck into learning C# (and how it works with S+) but you are covering a broad subject and there are some really fundamental things here that, once you get them nailed (conceptually) the rest will become much more obvious/mechanical.? Null references (if I'm right in guessing that's what this problem is) are a really common cause of errors in C-like languages and the latest C# and the VS IDE has lots of stuff to try to protect you from them, but at the end of the day, once you have an instinct for references/null and where it can all go wrong, it's all pretty straight forward.

I'd say that learning to interpret exception stack-traces is one of the most valuable tools.? When someone is staring blankly at a screen in our office, the answer they need is *often* right in front of them if they just read the exception type and stack trace carefully.? In your case, are you seeing another Null Reference Exception, with "ClearAllButtonFB()" at the top of the stack?

None of that is meant to sound patronising - hopefully it doesn't come across that way - and I hope it helps,
Oliver


 

Thanks for the advice. I have declared my array and set initial values

public? RootButtons[] _buttons = new RootButtons[16];

? ?public void Initialize()
? ? ? ? {
? ? ? ? ? ? try
? ? ? ? ? ? {?
? ? ? ? ? ? ? ? for (int i = 0; i < 16; i++)
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? _buttons[i] = new RootButtons
? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? BUTTON_FB = 0
? ? ? ? ? ? ? ? ? ? };
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? CrestronConsole.PrintLine("Initializing and StudioID received from SIMPL+ is {0} and Internal Value is {1}", MYSTUDIOID, MyStudioID);
? ? ? ? ? ? }
? ? ? ? ? ? catch (Exception e)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? CrestronConsole.PrintLine("Error Initializing module ---- {0}", e.Message);
? ? ? ? ? ? }
? ? ? ? }


 

Oliver - thanks for the help. I was able to isolate the issue with another method that the string variable hadn't been declared properly and was causing a Null Reference Exception.