By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,446 Members | 3,090 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,446 IT Pros & Developers. It's quick & easy.

Understanding Generics....

P: n/a

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
Share this Question
Share on Google+
7 Replies


P: n/a
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

P: n/a
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

P: n/a

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

P: n/a
"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

P: n/a
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

P: n/a

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

P: n/a

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 discussion thread is closed

Replies have been disabled for this discussion.