473,385 Members | 1,602 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Understanding Generics....


Hello NewsGroup,

Reading up on Generics in the .NET Framework 2.0 using C# 2005 (SP1), I have
a question on the application of Generics. Knowingly, Generic classes are
contained in the System.Collections.Generic namespace. Literature I have
read on this ties generics in with collections, hence articulate their
examples as such. That's fine, I understand what is being said.

My question is more towards the application and implementation of Generics.
Can it be applied to a class that is not apart of a collection? That is, in
the case where a class is not a part of a collection but, has several
overloaded functions. For example, take the following Win32 API wrapper
classes;

--------------------------------
Example 1: Without Generics.
--------------------------------
using System;
using System.Runtime.InteropServices;

namespace Win32API
{
namespace User32
{

public static class Messaging
{
///SendMessage is overloaded by the lParam parameter.

[DllImport("User32.dll", EntryPoint = "SendMessage", CharSet =
CharSet.Auto)]
private static extern IntPtr SendMessage(IntPtr hWnd, UInt32
uMsg, IntPtr wParam, IntPtr lParam);
public static IntPtr SenMessageApi(IntPtr hWnd, UInt32 uMsg,
IntPtr wParam, IntPtr lParam)
{
return SendMessage(hWnd, uMsg, wParam, lParam);
}

[DllImport("User32.dll", EntryPoint = "SendMessage", CharSet =
CharSet.Auto)]
private static extern IntPtr SendMessage(IntPtr hWnd, UInt32
uMsg, IntPtr wParam, String lParam);
public static IntPtr SenMessageApi(IntPtr hWnd, UInt32 uMsg,
IntPtr wParam, String lParam)
{
return SendMessage(hWnd, uMsg, wParam, lParam);
}

//Many more message api from User32...
}//public static class Messaging

public static class DeviceContext
{
//GetDCEx is overloaded by the flags parameter. Used for example
purposes only.
// Assume DeviceContextValues is an enum: uint defined.

[DllImport("User32.dll")]
private static extern IntPtr GetDCEx(IntPtr hWnd, IntPtr
hRgnClip, UInt32 flags);
public static IntPtr GetDCExApi(IntPtr hWnd, IntPtr hRgnClip,
UInt32 flags)
{
return GetDCEx(hWnd, hRgnClip, flags);
}

[DllImport("User32.dll")]
private static extern IntPtr GetDCEx(IntPtr hWnd, IntPtr
hRgnClip, DeviceContextValues flags);
public static IntPtr GetDCExApi(IntPtr hWnd, IntPtr hRgnClip,
DeviceContextValues flags)
{
return GetDCEx(hWnd, hRgnClip, flags);
}

// Many more device context functions from User32...
}//public static class DeviceContext

}//namespace User32

}//namespace Win32API

--------------------------------
Example 2: With Generics????
--------------------------------

Would this be a purpose of Generics? In this context would this be done this
way?

using System;
using System.Runtime.InteropServices;
using System.Collections.Generic;

namespace Win32API
{
namespace User32
{

public static class Messaging<T>
{
///SendMessage lParam parameter may be different types.

[DllImport("User32.dll", EntryPoint = "SendMessage", CharSet =
CharSet.Auto)]
private static extern IntPtr SendMessage(IntPtr hWnd, UInt32
uMsg, IntPtr wParam, T lParam);
public static IntPtr SenMessageApi(IntPtr hWnd, UInt32 uMsg,
IntPtr wParam, T lParam)
{
return SendMessage(hWnd, uMsg, wParam, lParam);
}

}//public static class Messaging

public static class DeviceContext<T>
{
//GetDCEx flags parameter maybe of different types.

[DllImport("User32.dll")]
private static extern IntPtr GetDCEx(IntPtr hWnd, IntPtr
hRgnClip, T flags);
public static IntPtr GetDCExApi(IntPtr hWnd, IntPtr hRgnClip, T
flags)
{
return GetDCEx(hWnd, hRgnClip, flags);
}

}//public static class DeviceContext

}//namespace User32

}//namespace Win32API

My apologies for the long post NewsGroup, I guess my main question is that,
can Generics in this context (i.e. classes not apart of a collection) be
used to "subdue" method overloading?

Many thanks NewsGroup, all the best for the New Year to you, and any
response will be deeply appreciated.

Regards,
SpotNet.
Dec 28 '06 #1
7 3228
SpotNet wrote:
>
Reading up on Generics in the .NET Framework 2.0 using C# 2005 (SP1), I have
a question on the application of Generics. Knowingly, Generic classes are
contained in the System.Collections.Generic namespace. Literature I have
read on this ties generics in with collections, hence articulate their
examples as such. That's fine, I understand what is being said.

My question is more towards the application and implementation of Generics.
Can it be applied to a class that is not apart of a collection? That is, in
the case where a class is not a part of a collection but, has several
overloaded functions.
Yes - generics can be used outside collection type classes.

Eksempel:

using System;

public class MyComparer<Twhere T : IComparable
{
public static T Max(T a, T b)
{
if(a.CompareTo(b) 0)
{
return a;
}
else
{
return b;
}
}
public static T Min(T a, T b)
{
if(a.CompareTo(b) < 0)
{
return a;
}
else
{
return b;
}
}
}

public class Test
{
public static void Main(string[] args)
{
Console.WriteLine(MyComparer<int>.Max(11,2));
Console.WriteLine(MyComparer<int>.Min(11,2));
Console.WriteLine(MyComparer<string>.Max("11","2") );
Console.WriteLine(MyComparer<string>.Min("11","2") );
}
}

Arne
Dec 28 '06 #2
SpotNet wrote:
For example, take the following Win32 API wrapper
classes;
I guess my main question is that,
can Generics in this context (i.e. classes not apart of a collection) be
used to "subdue" method overloading?
As shown in my previous reply, then generics can substitute
method overload.

But it does not work for you Win32 API example.

They have different number of arguments, they
need to call another method etc..

Arne
Dec 28 '06 #3

Thanks so much Arne,

I'm aware of the number of parameters and method calls, regarding this I am
not aiming to completely consolidate method overloading to one method that
does all types, but rather to "ease" up the number of similar methods I need
to define. In my actual class (not provided expamle) the SendMessage API has
about 6 different definitions on the lParam due to it being either; String,
StringBuilder, IntPtr, UInt32. This being the case I'll need only to define
this method twice if I use generics.

I will take your good advice though, I shall not use Generics in this
context.

Thanks again Arnie,

Regards,
SpotNet
"Arne Vajhøj" <ar**@vajhoej.dkwrote in message
news:45***********************@news.sunsite.dk...
: SpotNet wrote:
: For example, take the following Win32 API
wrapper
: classes;
:
: I guess my main question is that,
: can Generics in this context (i.e. classes not apart of a collection) be
: used to "subdue" method overloading?
:
: As shown in my previous reply, then generics can substitute
: method overload.
:
: But it does not work for you Win32 API example.
:
: They have different number of arguments, they
: need to call another method etc..
:
: Arne
Dec 28 '06 #4
"SpotNet" <Sp*****@msnews.grpwrote:
>I'm aware of the number of parameters and method calls, regarding this I am
not aiming to completely consolidate method overloading to one method that
does all types, but rather to "ease" up the number of similar methods I need
to define. In my actual class (not provided expamle) the SendMessage API has
about 6 different definitions on the lParam due to it being either; String,
StringBuilder, IntPtr, UInt32. This being the case I'll need only to define
this method twice if I use generics.
I will take your good advice though, I shall not use Generics in this
context.
That's a shame! Your idea seemed clever to me. I was hoping you'd
pursue it and write more in this newsgroup about how it turned out.

--
Lucian
Dec 28 '06 #5
SpotNet wrote:
Thanks so much Arne,

I'm aware of the number of parameters and method calls, regarding this I am
not aiming to completely consolidate method overloading to one method that
does all types, but rather to "ease" up the number of similar methods I need
to define. In my actual class (not provided expamle) the SendMessage API has
about 6 different definitions on the lParam due to it being either; String,
StringBuilder, IntPtr, UInt32. This being the case I'll need only to define
this method twice if I use generics.

I will take your good advice though, I shall not use Generics in this
context.

Thanks again Arnie,

Regards,
SpotNet
"Arne Vajhøj" <ar**@vajhoej.dkwrote in message
news:45***********************@news.sunsite.dk...
: SpotNet wrote:
: For example, take the following Win32 API
wrapper
: classes;
:
: I guess my main question is that,
: can Generics in this context (i.e. classes not apart of a collection) be
: used to "subdue" method overloading?
:
: As shown in my previous reply, then generics can substitute
: method overload.
:
: But it does not work for you Win32 API example.
:
: They have different number of arguments, they
: need to call another method etc..
:
: Arne

You can't have DllImports in a generic class. It's recommended that you
put your DllImports in a separate static class.

I actually tried your example, and it will give me a typeload exception
like this:

System.TypeLoadException: Generic method or method in generic class is
internal call, PInvoke, or is defined in a COM Import class.

HTH.
John Sun
separat
Dec 28 '06 #6

Thank you very much John, I should also try things for myself before posting
(such things anyway).

Best wishes for the New Year.

Regards,
SpotNet
"john sun" <js***********@gmail.comwrote in message
news:u6*************@TK2MSFTNGP06.phx.gbl...
: SpotNet wrote:
: Thanks so much Arne,
: >
: I'm aware of the number of parameters and method calls, regarding this I
am
: not aiming to completely consolidate method overloading to one method
that
: does all types, but rather to "ease" up the number of similar methods I
need
: to define. In my actual class (not provided expamle) the SendMessage API
has
: about 6 different definitions on the lParam due to it being either;
String,
: StringBuilder, IntPtr, UInt32. This being the case I'll need only to
define
: this method twice if I use generics.
: >
: I will take your good advice though, I shall not use Generics in this
: context.
: >
: Thanks again Arnie,
: >
: Regards,
: SpotNet
: >
: >
: "Arne Vajhøj" <ar**@vajhoej.dkwrote in message
: news:45***********************@news.sunsite.dk...
: : SpotNet wrote:
: : For example, take the following Win32 API
: wrapper
: : classes;
: :
: : I guess my main question is that,
: : can Generics in this context (i.e. classes not apart of a
collection) be
: : used to "subdue" method overloading?
: :
: : As shown in my previous reply, then generics can substitute
: : method overload.
: :
: : But it does not work for you Win32 API example.
: :
: : They have different number of arguments, they
: : need to call another method etc..
: :
: : Arne
: >
: >
: You can't have DllImports in a generic class. It's recommended that you
: put your DllImports in a separate static class.
:
: I actually tried your example, and it will give me a typeload exception
: like this:
:
: System.TypeLoadException: Generic method or method in generic class is
: internal call, PInvoke, or is defined in a COM Import class.
:
: HTH.
: John Sun
: separat
Dec 29 '06 #7

Thanks Lucian, would indeed be a good idea if it indeed worked. All the best
for the New Year.

Regards,
SpotNet

"Lucian Wischik" <lu***@wischik.comwrote in message
news:1s********************************@4ax.com...
: "SpotNet" <Sp*****@msnews.grpwrote:
: >I'm aware of the number of parameters and method calls, regarding this I
am
: >not aiming to completely consolidate method overloading to one method
that
: >does all types, but rather to "ease" up the number of similar methods I
need
: >to define. In my actual class (not provided expamle) the SendMessage API
has
: >about 6 different definitions on the lParam due to it being either;
String,
: >StringBuilder, IntPtr, UInt32. This being the case I'll need only to
define
: >this method twice if I use generics.
: >I will take your good advice though, I shall not use Generics in this
: >context.
:
: That's a shame! Your idea seemed clever to me. I was hoping you'd
: pursue it and write more in this newsgroup about how it turned out.
:
: --
: Lucian
Dec 29 '06 #8

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

Similar topics

27
by: Bernardo Heynemann | last post by:
How can I use Generics? How can I use C# 2.0? I already have VS.NET 2003 Enterprise Edition and still can´t use generics... I´m trying to make a generic collection myCollection<vartype> and...
2
by: Mr.Tickle | last post by:
So whats the deal here regarding Generics in the 2004 release and templates currently in C++?
23
by: Luc Vaillant | last post by:
I need to initialise a typed parameter depending of its type in a generic class. I have tried to use the C++ template form as follow, but it doesn't work. It seems to be a limitation of generics...
12
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...
5
by: anders.forsgren | last post by:
This is a common problem with generics, but I hope someone has found the best way of solving it. I have these classes: "Fruit" which is a baseclass, and "Apple" which is derived. Further I have...
11
by: herpers | last post by:
Hello, I probably don't see the obvious, but maybe you can help me out of this mess. The following is my problem: I created two classes NormDistribution and DiscDistribution. Both classes...
1
by: Vladimir Shiryaev | last post by:
Hello! Exception handling in generics seems to be a bit inconsistent to me. Imagine, I have "MyOwnException" class derived from "ApplicationException". I also have two classes...
2
by: RSH | last post by:
I have been looking at delegates lately. I have seen several articles explaining them but I'm having a hard time understanding why I would use a delegate over a traditional function or sub. Could...
13
by: rkausch | last post by:
Hello everyone, I'm writing because I'm frustrated with the implementation of C#'s generics, and need a workaround. I come from a Java background, and am currently writing a portion of an...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.