Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Friday, May 30, 2008 8:27 PM
Have a look at this function:
public T Add<T>(string varName) where T: struct
{
// Where dt is a class that can have int, float, string as values
return (T) dt.Value ;
}
Now this wouldn't work if T is of type String. And If I remove where T: struct, it wouldn't work at all. I need to be able to return int, float, and string data types. How can I achieve that?
TIA,
Kumar
Kumar
All replies (9)
Tuesday, June 3, 2008 9:17 AM âś…Answered
In that case, it cannot infer the parameter type from the argument.
But you can specify the type at the call site, like this:
class Program
{
static void Main(string[] args)
{
string s = Test<string>("string");
int i = Test<int>("int");
double d = Test<double>("double");
}
static object LookupThing(string thing)
{
switch (thing)
{
case "string": return "string";
case "int": return 1;
case "double": return 1.0;
default: throw new InvalidOperationException("pop");
}
}
static T Test<T>(string param)
{
return (T) LookupThing(param);
}
}
Saturday, May 31, 2008 8:34 AM
Perhaps you need to send a clearer example, because i have a hard time trying to understand what you're trying to achieve.
System.String is a reference type, thus the constraint T: struct would not be satisfied.
Removing the constraint would work, but leaves more options than only int, float and string.
Since you have only a limited range of allowed returntypes (and thus no need to be generic), you could implement three methods: AddInt, AddFloat and AddString.
Tim Van Wassenhove - Please remember to mark the replies as answers if they help.
Saturday, May 31, 2008 9:11 AM
OK; what us dt.Value?
If you are sure about the type, you can get around it (after removing the constraint) by casting to object as an intermediary:
return (T)(object)dt.Value;
The problem is that this will box and unbox any value-types. I have some more direct conversion code (that uses on-the-fly compilation and a few other tricks to avoid boxing), but it is more complex. So; if you tell us what "dt.Value" is, I might be able to suggest something more appropriate.
Marc
Monday, June 2, 2008 1:56 PM
What I am trying to achieve here is to write a generic function that returns both numeric types and string values. Is that possible?
TIA,
Kumar
Kumar
Monday, June 2, 2008 2:08 PM
Well yes:
class Program
{
static void Main(string[] args)
{
string s = Test("string");
int i = Test(1);
double d = Test(1.0);
}
public static T Test<T>(T param)
{
return param;
}
}
Monday, June 2, 2008 2:27 PM
See, my problem is the argument is not of T type. It is of string type. And finally, I need to type cast an object to T type.
In the example you gave here, the argument is of T type and so no issues there.
Thank you,
Kumar
Kumar
Tuesday, June 3, 2008 10:32 AM
Hi. Unless I misunderstand, you want to retrieve an object of the type that you specify by name.
| static object Add(string varName) |
| { |
| Type type = Type.GetType(varName); |
| return default(Type); |
| } |
Johan van Rhyn - Please remember to mark the replies as answers if you found them helpful.
Tuesday, June 3, 2008 11:14 AM
return default(Type);
That will always return null, the default for any reference-type (Type).
If I understand what you are trying to do, you'd have to use generics and MakeGenericMethod to do what you are trying to describe with that line... but I don't think it is what the OP is after.
Tuesday, June 3, 2008 1:09 PM
Thank you, Matthew. That will work.
Thank you,
Kumar
Kumar