?
Solved

C# Generic Method Error...

Posted on 2011-09-21
2
Medium Priority
?
207 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 2000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Suggested Courses

800 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