Understanding Generics....

S

SpotNet

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.
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

SpotNet said:
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<T> where 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
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

SpotNet said:
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
 
S

SpotNet

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


: 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
 
L

Lucian Wischik

SpotNet said:
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.
 
J

john sun

SpotNet said:
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


: 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
 
S

SpotNet

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


: 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
: >
: >
: > : > : 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
 
S

SpotNet

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

Regards,
SpotNet

: >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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top