How to return variable datatypes from a method
- by Praesagus
I have a method whose callers require different datatypes. I could repeat the requisite conversions a dozen times by putting them in each of the callers, but it seems much more efficient to do it once in the called method.
public String myMethod(String myArg)
{
return DoSomething(myArg);
}
public Int32 myMethod(String myArg)
{
return Convert.ToInt32(DoSomething(myArg));
}
private String DoSomething(key)
{
return SomeList[key];
}
If I have multiple methods that pull data from SomeList and have to utilize different data types then in each I have to do a type conversion. Examples might be session variables or query or form requests or any other number of things.
In VB I could say
Function myMethod(myArg as String) as Variant
myMethod=DoSomething(myArg)
End Function
Sorry if the original post was not very clear. I hope this makes more sense.
Thanks