473,551 Members | 2,794 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Question on using .NET Generics with C#

Hi, all,

Hopefully this will make sense:

I have 2 classes that implement the same generic interface.

public interface IAgingReport<T>
{
T GetAgingReport( DateTime dAsOfDate);
}
1 of the two classes that implements the interface, returns a dataset:

public class bzAgingReport : IAgingReport<Da taset>
{
public Dataset GetAgingReport( DateTime dAsOfDate);
{
// get data for aging report, return dataset
}
}
The 2nd class also implements the interface, but returns an XML
string:

public class wAgingReport : WebService, IAgingReport<st ring>
{
[WebMethod]
public string GetAgingReport( DateTime dAsOfDate)
{
// return XML string

}

The reason I'm using one interface is because the client piece might
be using web services, or it might be using remoting. I want to use
an interface-based approach on the client-side, and use a common code
base, regardless of which communication approach is being used.

On the client side, I can create a regular .NET object (which is
either a web reference object or a remoting object), but I can't
figure out how to write "generic" code to deal with the different data
types that might be coming back (XML string or dataset).

So in my client piece, I have (and this is pseudocode)

object oReturnObject = either web service object or remoting
proxy object
if (using web services)
{
IAgingReport<st ringoAgingRepor t;
oAgingReport = (IAgingReport<s tring>)oReturnO bject;
string cXMLResults =oAgingReport.G etAgingReport(d AsOfDate);
MyDataSet.ReadX ml(new StringReader(cX MLResults));
}
else
{
IAgingReport<Da taSetoAgingRepo rt;
oAgingReport = (IAgingReport<D ataSet>)oReturn Object;
MyDataSet = oAgingReport.Ge tAgingReport(dA sOfDate);
}
// at this point, either way, I have my results in a dataset

Essentially, in the first example, I'm utilizing the interface
function that returns a string (and then convert to a dataset). In
the second example, I'm using the interface function that returns a
dataset.

here's my question - this all "works", but I've been trying to figure
out how to avoid the 'IF' block above. I'm wondering if I need a 2nd
usage of generics, but I can't figure out which one. I've tried
different combinations of specifying a variable type, but nothing I
try seems to work.

Thanks in advance,
Kevin


Aug 3 '06 #1
1 1667
Hi Kevin,
I'm not sure what you're trying to acheive by using generics in this
situation, as I'd probably have chosen (given the information in your
post) to skip generics and just create one object that returns a
DataSet (assuming the XML string conforms to some DataSet schema).
Since DataSet instances are serializable (as XML) you'd be able to
reference the same server-side object over both transport layers. Using
generics only seems to compound the issues you're seeing, by not being
able to cast between IAgingReport<Da tasetand IAgingReport<st ring>.
Hope this helps,
Jono

Kevin S. Goff wrote:
Hi, all,

Hopefully this will make sense:

I have 2 classes that implement the same generic interface.

public interface IAgingReport<T>
{
T GetAgingReport( DateTime dAsOfDate);
}
1 of the two classes that implements the interface, returns a dataset:

public class bzAgingReport : IAgingReport<Da taset>
{
public Dataset GetAgingReport( DateTime dAsOfDate);
{
// get data for aging report, return dataset
}
}
The 2nd class also implements the interface, but returns an XML
string:

public class wAgingReport : WebService, IAgingReport<st ring>
{
[WebMethod]
public string GetAgingReport( DateTime dAsOfDate)
{
// return XML string

}

The reason I'm using one interface is because the client piece might
be using web services, or it might be using remoting. I want to use
an interface-based approach on the client-side, and use a common code
base, regardless of which communication approach is being used.

On the client side, I can create a regular .NET object (which is
either a web reference object or a remoting object), but I can't
figure out how to write "generic" code to deal with the different data
types that might be coming back (XML string or dataset).

So in my client piece, I have (and this is pseudocode)

object oReturnObject = either web service object or remoting
proxy object
if (using web services)
{
IAgingReport<st ringoAgingRepor t;
oAgingReport = (IAgingReport<s tring>)oReturnO bject;
string cXMLResults =oAgingReport.G etAgingReport(d AsOfDate);
MyDataSet.ReadX ml(new StringReader(cX MLResults));
}
else
{
IAgingReport<Da taSetoAgingRepo rt;
oAgingReport = (IAgingReport<D ataSet>)oReturn Object;
MyDataSet = oAgingReport.Ge tAgingReport(dA sOfDate);
}
// at this point, either way, I have my results in a dataset

Essentially, in the first example, I'm utilizing the interface
function that returns a string (and then convert to a dataset). In
the second example, I'm using the interface function that returns a
dataset.

here's my question - this all "works", but I've been trying to figure
out how to avoid the 'IF' block above. I'm wondering if I need a 2nd
usage of generics, but I can't figure out which one. I've tried
different combinations of specifying a variable type, but nothing I
try seems to work.

Thanks in advance,
Kevin
Aug 3 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
1659
by: Matthew W. Jackson | last post by:
I had a question about the "using" statement and Generics in the next version of C#, and I was directed to this newsgroup. My question is: Will the following syntax be valid? using Int32ArrayList = System.GCollections.ArrayList<Int32>; .... Int32ArrayList myArrayList = new Int32ArrayList();
16
1810
by: bigtexan | last post by:
I would like to do the following and cannot figure it out. public class A<T> { public delegate T GetValueDelegate(A<T> var); public GetValueDelegate GetValue = new GetValueDelegate(B.GetValue); } public class B {
12
2724
by: Michael S | last post by:
Why do people spend so much time writing complex generic types? for fun? to learn? for use? I think of generics like I do about operator overloading. Great to have as a language-feature, as it defines the language more completely. Great to use.
1
1882
by: Peter Kirk | last post by:
Hi I have never used generics before, and I was wondering if the following sort of use was acceptable/normal for a method: public IList<IPerson> GetPersons() { IList<IPerson> personList = new List<IPerson>(); ... // get the persons return personList;
10
1774
by: Lloyd Dupont | last post by:
Let say I have 2 methods: void BeginGroup(); void BeginGroup(string msg); when I want to refer to them I write /// <see cref="BeginGroup"/> But this cause a compiler warning, where my declaration is ambiguous (between the 2 BeginGroup methods). But what if I want to refer them both?
11
2463
by: hammad.awan_nospam | last post by:
Hello, I'm wondering if it's possible to do the following with Generics: Let's say I have a generic member variable as part of a generic class like this: List<DLinqQuery<TDataContext>> _queries; where DLinqQuery is a generic class that takes a type parameter
14
1691
by: cwineman | last post by:
Hello, I'm hoping to do something using Generics, but I'm not sure it's possible. Let's say I want to have a bunch of business objects and a data access class cooresponding to each business object. For each business object, I would have something like below: public class Apple { //apple stuff
4
4208
by: DeveloperX | last post by:
I'm having a play with EventHandlerList but the documentation is a bit ropey and I can't find any decent examples. It also doesn't seem to do what I was led to believe it would. I was under the impression that windows.forms controls used EventHandlerLists because generally most events aren't consumed so this saves memory. I can add a button...
11
1529
by: Olaf Krumnow | last post by:
Hi, I have a requirement that, I thought, was very simple, but in fact turned out as impossible for me. I read some posts here and the language specs from MS, but couldn't get a solution. I coded the part in java, because it's very straightforward there:
0
7565
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7492
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7768
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8002
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
6106
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5406
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5130
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
1
1981
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
804
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.