Switch Statement & Objects

  • Thread starter Thread starter chis2k
  • Start date Start date
C

chis2k

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

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

this throws errors....
 
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.
switch (tx.Name) {
case "textbox2": /*something*/ break;
case "textbox3": /*something*/ break;
}

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
 
Back
Top