Solved

C# Generic Method Error...

Posted on 2011-09-21
2
205 Views
Last Modified: 2012-08-13
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

0
Comment
Question by:hpdvs2
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 12

Accepted Solution

by:
andrewjb earned 500 total points
ID: 36574608
Should be

        public static Result<T> QueryValue<T>(string sql)
0
 
LVL 8

Author Closing Comment

by:hpdvs2
ID: 36574704
BAAAHHH!!!  (facepalm) I can't believe I forgot to put that in.
0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applicationsā€¦
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decadeā€¦

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question