473,545 Members | 1,884 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 1665
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
1658
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
2723
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
1881
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
1770
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
2461
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
1690
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
4204
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
7478
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
7668
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
7923
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...
1
7437
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7773
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
3466
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3448
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1901
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
1
1025
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.