How to return variable datatypes from a method

Posted by Praesagus on Stack Overflow See other posts from Stack Overflow or by Praesagus
Published on 2009-11-30T22:33:05Z Indexed on 2010/03/11 20:44 UTC
Read the original article Hit count: 352

Filed under:
|

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

© Stack Overflow or respective owner

Related posts about c#

Related posts about ASP.NET