Delegate Question

  • Thread starter Thread starter Doug
  • Start date Start date
D

Doug

I have a delegate and it works fine, but I'm having difficulty using it w/ a
switch statement.

Basically, i have a component that has a listbox with a delegate for the
SelectedIndexChanged event. On the main form where i'm capturing the event
I want to use a switch statement to see what the user selected. The
"sender" has the value (based on the debugger) {SelectedItem="my value"}.
How do I capture this in a switch statement?

Doug
 
Doug said:
How do I capture this in a switch statement?

ListBox list = sender as ListBox;
if (list != null) {
switch (list.SelectedItem.Text) {
case "Item 1":
//yada, yada, yada
}
}

--
There are 10 kinds of people. Those who understand binary and those who
don't.

http://code.acadx.com
(Pull the pin to reply)
 
Hi Doug,
It depends on what the type of the items in the list box is.
Don't forget that you can use only primitive types with *switch* statement.

So if you have strings as ListBox items you can do the following

switch(((ListBox)sender).SelectedItem.ToString())
{
case "string1":
break;
case "string2":
.....
}

Even if you have some non-primitive objects you can override ToString()
method and if you can produce some strings do distinguish the objects you
can use the same *switch* statement.

You always can use *if...else if...else* insted, though.

B\rgds
100
 
Back
Top