Functions with parameters of type TextBox and Buttons

4

I have a program that at one point works with the visibility of an object. I'm creating a function and I do not know how to reference objects.

private void Visible(object x, object y, object z, object a) 
{    
    x.Visible = true;
    y.Visible = true;
    z.Visible = true;
    a.Visible = true;
}

The .Visible are in red underline, before I was doing it manually, but with function it decreases the amount of lines of code. How do I reference TextBoxes and Buttons ?

    
asked by anonymous 28.05.2015 / 16:57

3 answers

8

All components that have visual representation inherit from the Control .

Change your method to:

private void Visible(Control x, Control y, Control z, Control a) {

    x.Visible = true;
    y.Visible = true;
    z.Visible = true;
    a.Visible = true;  
}
    
28.05.2015 / 17:03
7

Complementing @ramaral's response, I think this is a good opportunity to use params .

private static void Visible(params Control[] controls)
{
    foreach(var control in controls)
        control.Visible = true;
}

So you can pass a variable number of controls, and avoid repeating code within the Visible method:

// uso
Visible(x, y, z, a);

More information

    
28.05.2015 / 17:12
3

The problem is that you are trying to set the property on an object of type object , not TextBox .

You have to do cast for the type you want, for example:

((TextBox)x).Visible = true;

or

((Button)y).Visible = true;
    
28.05.2015 / 17:01