Link to home
Start Free TrialLog in
Avatar of Dan Violet Sagmiller (He/Him)
Dan Violet Sagmiller (He/Him)Flag for United States of America

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?
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 = "";
    }
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of andrewjb
andrewjb
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Dan Violet Sagmiller (He/Him)

ASKER

BAAAHHH!!!  (facepalm) I can't believe I forgot to put that in.