Dan Violet Sagmiller (He/Him)
asked on
C# Generic Method Error...
I have the attached fragment of code, and in it, I am using generics in a method to define to type of data being returned from a database. It errors, complaining that :
"The type or namespace name 'T' could not be found (are you missing a using directive or an assembly reference?"
I've seen functional samples where the method is the only point that uses generics, and the class itself does not.
Whats going wrong with my code?/how do a make a method return a generic type that is not called in the class instantiation?
"The type or namespace name 'T' could not be found (are you missing a using directive or an assembly reference?"
I've seen functional samples where the method is the only point that uses generics, and the class itself does not.
Whats going wrong with my code?/how do a make a method return a generic type that is not called in the class instantiation?
class Access
{
private static SqlCeConnection connection;
public static Result<T> QueryValue(string sql)
{
Result<T> result = new Result<T>();
SqlCeCommand command = new SqlCeCommand(sql, connection);
try
{
result.Value = (T)command.ExecuteScalar();
}
catch (Exception e)
{
result.Comments = "Unable to obtain value, the results threw an exception";
result.ExceptionInfo = e;
return result;
}
result.Successful = true;
return result;
}
}
public class Result<T>
{
public T Value;
public bool Successful = false;
public Exception ExceptionInfo = null;
public string Comments = "";
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER