简体   繁体   中英

C# - Is Statement in Switch Statement

I need to check if a large number of controls contain values or whether they have been left blank.

I was hoping to do something like this:

public static bool IsObjectEmpty(Control ctrlThis)
    {
        switch (ctrlThis)
        {
            case ctrlThis is TextBox:
                TextBox txtThis = (TextBox)ctrlThis;
                if (txtThis.Text == "" || txtThis.Text == null)
                { return false; }
                break;

            case (ctrlThis is ComboBox):
                ComboBox cboThis = (ComboBox)ctrlThis;
                if (cboThis.SelectedValue == -1)
                { return false; }
                break;

            case (ctrlThis is NumericUpDown):
                NumericUpDown numThis = (NumericUpDown)ctrlThis;
                if (numThis.Value == 0)
                { return false; }
                break;

            etc etc...

But this doesn't compile:

 Error  3   A switch expression or case label must be a bool, char, string,    
 integral, enum, or corresponding nullable type

Is there a way of doing this in a switch statement, or am I just gonna have to do a load of if / else if stuff? Google and StackOverflow searches haven't turned up much of any use.

Case labels can contains only constant expression.

so in your answer is is not a const expression.

it is an evaluated value.

just as much as you can't do

public const int a=Enumerable.Range(0,2).First();

you can calc those value before the switch case

and then compare them to a value.

something like

var t=(ctrlThis is ComboBox)

...
...

switch ( t) ...

case  true :...

edit:from the CLS

switch-label:
    case constant-expression :
    default :

if you dont do it like that the compiler will scream :

A constant value is expected

example :

switch (myInt)
{
case (2+Enumerable.Range(0,2).First()):
    return true;
    default:
    return true;
}

Conditions (if/switch) based on type are mostly a bad idea.

How about this approach:

public static bool IsObjectEmpty(TextBox ctrlThis)
{
    if (ctrlThis.Text == "" || ctrlThis.Text == null) {
        return false;
    }

    etc etc...
}            

public static bool IsObjectEmpty(ComboBox ctrlThis)
{
    if (ctrlThis.SelectedValue == -1) {
        return false;
    }

    etc etc...
}            

public static bool IsObjectEmpty(NumericUpDown ctrlThis)
{
    if (ctrlThis.Value == 0) {
        return false;
    }

    etc etc...
}

You can do:

switch (ctrlThis.GetType().ToString())
{
    case "System.Windows.Forms.TextBox" :
            TextBox txtThis = (TextBox)ctrlThis;
                if (txtThis.Text == "" || txtThis.Text == null)
                { return false; }
                break;
}

I might go for if-statements aswell, such as:

    public static bool IsObjectEmpty(Control ctrlThis)
    {
        if (ctrlThis is TextBox)
        {
            TextBox txtThis = (TextBox)ctrlThis;
            if (txtThis.Text == "" || txtThis.Text == null)
                return false;
        }
        else if (ctrlThis is ComboBox)
        {
            ComboBox cboThis = (ComboBox)ctrlThis;
            if (int.Parse(cboThis.SelectedValue.ToString()) == -1)
                return false;
        }
        else if (ctrlThis is NumericUpDown)
        {
            NumericUpDown numThis = (NumericUpDown)ctrlThis;
            if (numThis.Value == 0)
                return false;
        }
        else
        {
            //Serves as 'default' in the switch
        }
        return true;
    }

As far as I know you can do it like this

public static bool IsObjectEmpty(Control ctrlThis)
{
    Type t = ctrlThis.GetType();
    switch (t)
    {
        case typeof(TextBox):
            TextBox txtThis = (TextBox)ctrlThis;
            if (txtThis.Text == "" || txtThis.Text == null)
            { return false; }
            break;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM