Tuesday, 3 September 2013

Generic collection of MenuItems in C#?

Generic collection of MenuItems in C#?

I am working on displaying a list of audio input devices in the menu, and
I'm fairly new to C#. The number of input devices not only varies from
machine to machine, it is possible that someone might add or subtract a
USB device while the program is running. I wrote code that checks whenever
the menu is activated, but I have to limit the number of possible input
devices. It's not likely that there will be more than 10 input devices,
but, in order to understand C# better, I'd like to see if it's possible to
use a generic so that I don't have to limit the list.
Here's what I have going for code now:
MainMenu sgFileMenu = new MainMenu();
List<MenuItem> inputDevice = new List<MenuItem>();
MenuItem myMenuItemInput = new MenuItem("&Input Devices");
sgFileMenu.MenuItems.Add(myMenuItemInput);
for (int i = 0; i < deviceCount; i++)
{
myMenuItemInput.MenuItems.Add(inputDevice[i]);
}
That compiles but gives an ArgumentOutOfRange exception when I run it. I'm
obviously missing something about how generics are set up- can somebody
clue me in?

1 comment: