Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Variant Type in C#

Tags:

c#

types

VBA (and I assume VB) has a Variant type which I believe takes up more memory, but covers various data types.

Is there an equivalent in c# ?

In a windows form say I had the following, how would I amend the type of z so that it runs ok

    private void uxConvertButton_Click(object sender, EventArgs e)
    {
        int x = 10;

        byte j = (byte)x;
        upDateRTB(j);

        long q = (long)x;
        upDateRTB(q);

        string m = x.ToString();
        upDateRTB(m);
    }

    void upDateRTB(long z) {
        MessageBox.Show(this,"amount; "+z);
    }
like image 964
whytheq Avatar asked Jun 15 '12 07:06

whytheq


2 Answers

void upDateRTB(object z) {
    MessageBox.Show(this, "amount; " + Convert.ToString(z));
}
like image 155
Darin Dimitrov Avatar answered Oct 07 '22 07:10

Darin Dimitrov


An object parameter would accept all, but if you'd like to keep the variables strongly typed (and avoid boxing in the process), you could use generics:

void upDateRTB<T>(T z) {
    MessageBox.Show(this,"amount; "+ Convert.ToString(z)); 
}

The method calls could remain precisely the same, because the compiler can resolve the generic type based on the given parameter.

like image 32
Me.Name Avatar answered Oct 07 '22 08:10

Me.Name