Switch Statement & Objects

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....
 
J

Jay B. Harlow [MVP - Outlook]

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top