4

I am making some validation functions for my project but I am stuck on something. I want to have a single function to handle a couple of different controls and errors.
Here's my code:

private void ValidateControls(Control c)
{
    if (c is TextBox)
    {
        if (c.Text == "")
        {
            epNew.SetError(c, "Something");
        }
    }
    else if (c is ComboBox)
    {
        // What now?
        // if (c.SelectedItem == null) does not work
    }

}

And I am calling it like this:

private void txtNEAN_Validating(object sender, CancelEventArgs e)
{
    ValidateControls(txtNEAN);
}

This works fine for textboxes. But if I do:

private void cbbEMerk_Validating(object sender, CancelEventArgs e)
{
    ValidateControls(cbbEMerk);
}

if (c.SelectedItem == null) for example does not work.
How can I achieve this? And is this okay to use? If not, what is a better alternative?
I'd love to hear anything!

Green Falcon
  • 818
  • 3
  • 17
  • 48
Sj03rs
  • 937
  • 10
  • 32

3 Answers3

6

You have to cast c to a ComboBox in this case

else if (c is ComboBox)
     {
         if (((ComboBox)c).SelectedItem == null)
     }

By the way, don't create a _Validating method for every control if they do the same thing. You can use a single one, or one txtBox_Validating for TextBoxes, one comboBox_Validating for comboboxes, etc.

lime
  • 6,901
  • 4
  • 39
  • 50
Raphaël Althaus
  • 59,727
  • 6
  • 96
  • 122
  • Thanks for the suggestions and the answer! I will mark it as soon as I can. – Sj03rs Sep 20 '16 at 08:58
  • 1
    You may also read https://msdn.microsoft.com/en-us/library/ms950965.aspx (not sure if you're working with WinForms, but anyway) – Raphaël Althaus Sep 20 '16 at 08:59
  • Also, don't you need to cast it like `if (((ComboBox)c).SelectedItem == null)`? Otherwise it will give me an error that ComboBox can not be used as a method. – Sj03rs Sep 20 '16 at 08:59
2

Try using

((ComboBox)c).SelectedItem

instead. This tells the program to parse the Control c into a ComboBox.

As an alternative, instead of using is you could use as

// Converts c to a ComboBox. If c is not a ComboBox, assigns null to cmbControl

ComboBox cmbControl = c as ComboBox; 
if (cmbControl != null)
{
    if (cmbControl.SelectedItem != null)
    {
        // Do stuff here
    }
}
// Else it's not a ComboBox
marksfrancis
  • 1,722
  • 1
  • 13
  • 14
1

It is also good to know about safety cast using as and is:

Because objects are polymorphic, it is possible for a variable of a base class type to hold a derived type. To access the derived type's method, it is necessary to cast the value back to the derived type. However, to attempt a simple cast in these cases creates the risk of throwing an InvalidCastException. That is why C# provides the is and as operators. You can use these operators to test whether a cast will succeed without causing an exception to be thrown. In general, the as operator is more efficient because it actually returns the cast value if the cast can be made successfully. The is operator returns only a Boolean value. It can therefore be used when you just want to determine an object's type but do not have to actually cast it.


You can see more here

Green Falcon
  • 818
  • 3
  • 17
  • 48