Solved

C# Generic Method Error...

Posted on 2011-09-21
2
201 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
2 Comments
 
LVL 12

Accepted Solution

by:
andrewjb earned 500 total points
Comment Utility
Should be

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

Author Closing Comment

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

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
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.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

762 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

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now