Reputation: 383
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.
Upvotes: 3
Views: 1708
Reputation: 515
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;
}
Upvotes: 1
Reputation: 121
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;
}
}
Upvotes: 0
Reputation: 26330
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...
}
Upvotes: 2
Reputation: 148524
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 :...
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;
}
Upvotes: 2
Reputation: 46919
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;
}
Upvotes: 1