Switch Statement & Objects

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

    #1

    Switch Statement & Objects

    How can I compare objects in a switch statement? For instance:

    public void textbox1_Select (object sender, System.Eventarg s e) {
    Textbox tx = (TextBox)sender ;
    switch (tx) {
    case textbox2: /*something*/ break;
    case textbox3: /*something*/ break;
    }
    }

    this throws errors....


  • Jay B. Harlow [MVP - Outlook]

    #2
    Re: Switch Statement & Objects

    chris2k,
    The switch statement only supports numbers & strings, it does not support
    objects.

    You could either use the Name property of your textboxes, and switch on
    that.
    [color=blue]
    > switch (tx.Name) {
    > case "textbox2": /*something*/ break;
    > case "textbox3": /*something*/ break;
    > }[/color]

    Or you could use an cascading if statements (if else if else if else if).

    if (tx is textbox2)
    { }
    else if (tx is textbox3)
    { }

    Hope this helps
    Jay

    "chis2k" <sfd@sdf.com> wrote in message
    news:OSzVaqgYDH A.2592@TK2MSFTN GP09.phx.gbl...[color=blue]
    > How can I compare objects in a switch statement? For instance:
    >
    > public void textbox1_Select (object sender, System.Eventarg s e) {
    > Textbox tx = (TextBox)sender ;
    > switch (tx) {
    > case textbox2: /*something*/ break;
    > case textbox3: /*something*/ break;
    > }
    > }
    >
    > this throws errors....
    >
    >[/color]


    Comment

    Working...