Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Returning int, string, logical, ? from method
Message
General information
Forum:
ASP.NET
Category:
Coding, syntax and commands
Miscellaneous
Thread ID:
01319040
Message ID:
01319071
Views:
16
>Paul,
>
>Why we can not use Generics here? The method will return an object of specified type and also will receive a parameter of the specified type.
>

He wants to be able to pass in a string, generics wouldn't help here. You can't have a method who's signature only differs by return type. Generics also doesn't automagically convert types for you, so assuming this is coming from a configuration file, he has to cast the string back into the original type.

However, he could also create a number of overloaded methods and pass a parameter by reference which gets filled in:
// Sample Call
int testInt = 0;
SettingsManager("SomeProperty", out testInt);

string testString = "";
SettingsManager("SomeOtherProperty", out testString);

// Class
public class SettingsManager
{
   public static void GetValue(string propertyName, out int returnValue)
   {
	   returnValue = 1;       
   }

   public static void GetValue(string propertyName, out string returnValue)
   {
       returnValue = "Hello";
   }
}
It's a little ugly, but it would work. I'd still prefer the strongly-typed class interface instead.
-Paul

RCS Solutions, Inc.
Blog
Twitter
Previous
Reply
Map
View

Click here to load this message in the networking platform