Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
|
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 29th, 2006, 08:01 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 217
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to mega
Default Generic return type

 Hi all.
I've already posted this in the visual C# 2005 forum but I thought it was more appropriate to put it here.
Does anyone know how I can define a method that can return both a DataTable and a int or string?
I've tried this:


internal interface IQuery
{
        void OpenConnection();
        void CloseConnection();
        T DoQuery<T>();
}
abstract internal class QueryClass : IQuery
{
...
    public abstract T DoQuery<T>();
}
class OpenQuery : QueryClass
{
     public override T DoQuery<T>()
     {
       DataTable dt = new DataTable();
            try
            {
                try
                {
                    this.OpenConnection();
                    dt.Load(Command.ExecuteReader());

                    return dt;
...
                }
            }
     }
}
...
OpenQuery query = new OpenQuery(queryname, parameters);
return query.DoQuery<DataTable>();


Obviously I can't return a DataTable when the return type is not defined in the DoQuery method and I can't call Load on a generic type. How do you guys work around this??

 - mega
Moving to C# .NET
__________________
- mega
Aspiring JavaScript Ninja
 
Old November 29th, 2006, 11:07 PM
Authorized User
 
Join Date: Sep 2006
Posts: 66
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi ,Why cant u try returning of type "OBJECT".

 
Old November 30th, 2006, 07:57 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 217
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to mega
Default

I could but then I would have to cast the DataTable to an object when I return it from the DoQuery and then cast it back to a DataTable again when I want to use it in the caller code. That can't be good for performance. I'm thinking there is no way this can be done in a strong type way.

 - mega
Moving to C# .NET
 
Old November 30th, 2006, 09:48 AM
Authorized User
 
Join Date: Sep 2006
Posts: 66
Thanks: 0
Thanked 0 Times in 0 Posts
Default

HI , ok , U can create a structure with two variables of type one datatable and second int .Now U can use this structure as a return Type .

Good Luck


 
Old November 30th, 2006, 11:00 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 217
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to mega
Default

Yeah, that would do it. But for this project I followed your advise and defined the method return type as object. I think it's the best choice out of poor possibilities.

 - mega
Moving to C# .NET





Similar Threads
Thread Thread Starter Forum Replies Last Post
Generic return type mega C# 2005 1 November 30th, 2006 07:27 AM
return type? Sibananda Tripathy J2EE 1 January 3rd, 2005 04:03 AM
Generic Type specification during runtime ChronicCode General .NET 2 December 3rd, 2004 01:51 AM
Generic Type specification during runtime ChronicCode .NET Framework 2.0 0 December 2nd, 2004 12:55 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.