• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 214
  • Last Modified:

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);
                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

1 Solution
Should be

        public static Result<T> QueryValue<T>(string sql)
hpdvs2Author Commented:
BAAAHHH!!!  (facepalm) I can't believe I forgot to put that in.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now