Home > OS >  C# - Call an object as argument of a function
C# - Call an object as argument of a function

Time:01-02

Happy New Year.

I am struggling to find a way to call an object in a function by using objects in the arguments. It is all about trying to translate a code I made in VB into C# but it does not sound that straightforward.

I tried to turn this :

            test = textbox_fill(txtbox_value, txtbox_object, 8, &H80000012)

[...]

Public Function textbox_fill(textbox_value_string As String, textbox_object As Object, value As String, text_forecolor_value As Double) As Boolean
    textbox_value_string = value
    textbox_object.Text = value
    textbox_object.ForeColor = text_forecolor_value
    textbox_fill = True
End Function

Into this in C#:

textbox_fill(txtbox_value, txtbox8object, "8", 0x80000012);
        bool textbox_fill(string textbox_value_string, object textbox_object as Object, string value, double text_forecolor_value)
        {
            textbox_value_string = value;
            textbox_object.Text = value;
            textbox_object.ForeColor = text_forecolor_value;
            return true;
        }

What I have done is the following but it displays errors for the .Text and .Forecolor.

Thanks for your support.

CodePudding user response:

The problem is that you have Option Strict Off in VB (I strongly recommend to set it On either on the top of all the VB files or in the project settings). Option Strict Off allows late binding and implicit typing. C# works always as it had Option Strict On, Option Explicit On and Option Infer On with the var keyword (C# doesn't have such a switches). Because an object does not have a Text or a ForeColor property, you cannot access them. Even if you assign a TextBox (or another Control to it.

If you intend to pass a TextBox as argument, then type this parameter as TextBox, because TextBox is a class and therefore a type:

bool textbox_fill(string text, TextBox textbox, string value, double forecolor)

Also, ForeColor is a System.Drawing.Color. Not a double. Note that TextBox inherits both of these properties from Control. Therefore, you could also type the parameter as Control. This would allow you to use this function with other types of controls.

bool FillControl(Control control, string text, Color foreColor)
{
    control.Text = text;
    control.ForeColor = foreColor;
    return true;
}

You can get a Color from numbers with various overloads of the Color.FromArgb Method.

CodePudding user response:

Why do you want to give txtbox8object(name of your textbox) as input to the function when you have access to it? try this:

    bool textbox_fill(string textbox_value_string, string value, double text_forecolor_value)
    {
        txtbox8object.Text = value;
        txtbox8object.ForeColor = text_forecolor_value;
        return true;
     }
        

CodePudding user response:

OK! After trying various things, I think I have found it. It seems to actually work.

bool textbox_fill(string textbox_value_string, TextBox textbox_object, string value, int text_forecolor_value)
{
    textbox_value_string = value;
    textbox_object.Text = value;
    textbox_object.ForeColor = Color.FromArgb(text_forecolor_value);
    return true;
}

Note : I changed the type for the color because the way the color are coded is not exactly the same in VB, at least, not the one I used so far. Hope it will be useful to other people too.

  • Related