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