Delegate Question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Doug

    Delegate Question

    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
    SelectedIndexCh anged 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


  • Frank Oquendo

    #2
    Re: Delegate Question

    Doug wrote:[color=blue]
    > How do I capture this in a switch statement?[/color]

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

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


    (Pull the pin to reply)


    Comment

    • 100

      #3
      Re: Delegate Question

      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(((ListBo x)sender).Selec tedItem.ToStrin g())
      {
      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

      "Doug" <dkhandler@_nos pam_hotmail.com > wrote in message
      news:eJwZu7DuDH A.2224@TK2MSFTN GP09.phx.gbl...[color=blue]
      > I have a delegate and it works fine, but I'm having difficulty using it w/[/color]
      a[color=blue]
      > switch statement.
      >
      > Basically, i have a component that has a listbox with a delegate for the
      > SelectedIndexCh anged event. On the main form where i'm capturing the[/color]
      event[color=blue]
      > 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
      >
      >[/color]


      Comment

      Working...