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);
}
void upDateRTB(object z) {
MessageBox.Show(this, "amount; " + Convert.ToString(z));
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With