473,729 Members | 2,149 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.Collecti ons.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("User 32.dll", EntryPoint = "SendMessag e", CharSet =
CharSet.Auto)]
private static extern IntPtr SendMessage(Int Ptr hWnd, UInt32
uMsg, IntPtr wParam, IntPtr lParam);
public static IntPtr SenMessageApi(I ntPtr hWnd, UInt32 uMsg,
IntPtr wParam, IntPtr lParam)
{
return SendMessage(hWn d, uMsg, wParam, lParam);
}

[DllImport("User 32.dll", EntryPoint = "SendMessag e", CharSet =
CharSet.Auto)]
private static extern IntPtr SendMessage(Int Ptr hWnd, UInt32
uMsg, IntPtr wParam, String lParam);
public static IntPtr SenMessageApi(I ntPtr hWnd, UInt32 uMsg,
IntPtr wParam, String lParam)
{
return SendMessage(hWn d, 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 DeviceContextVa lues is an enum: uint defined.

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

[DllImport("User 32.dll")]
private static extern IntPtr GetDCEx(IntPtr hWnd, IntPtr
hRgnClip, DeviceContextVa lues flags);
public static IntPtr GetDCExApi(IntP tr hWnd, IntPtr hRgnClip,
DeviceContextVa lues 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.Collecti ons.Generic;

namespace Win32API
{
namespace User32
{

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

[DllImport("User 32.dll", EntryPoint = "SendMessag e", CharSet =
CharSet.Auto)]
private static extern IntPtr SendMessage(Int Ptr hWnd, UInt32
uMsg, IntPtr wParam, T lParam);
public static IntPtr SenMessageApi(I ntPtr hWnd, UInt32 uMsg,
IntPtr wParam, T lParam)
{
return SendMessage(hWn d, uMsg, wParam, lParam);
}

}//public static class Messaging

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

[DllImport("User 32.dll")]
private static extern IntPtr GetDCEx(IntPtr hWnd, IntPtr
hRgnClip, T flags);
public static IntPtr GetDCExApi(IntP tr 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 3256
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.Collecti ons.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<Twhe re 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.WriteLi ne(MyComparer<i nt>.Max(11,2));
Console.WriteLi ne(MyComparer<i nt>.Min(11,2));
Console.WriteLi ne(MyComparer<s tring>.Max("11" ,"2"));
Console.WriteLi ne(MyComparer<s tring>.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.d kwrote in message
news:45******** *************** @news.sunsite.d k...
: 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,
StringBuilde r, 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.d kwrote in message
news:45******** *************** @news.sunsite.d k...
: 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.TypeLoad Exception: 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******** *****@TK2MSFTNG P06.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.d kwrote in message
: news:45******** *************** @news.sunsite.d k...
: : 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.TypeLoad Exception: 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.c om...
: "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,
: >StringBuilde r, 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
2460
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 still no can do... Any info would be great!
2
3103
by: Mr.Tickle | last post by:
So whats the deal here regarding Generics in the 2004 release and templates currently in C++?
23
2547
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 vs C++ templates. Does anyone knows a workaround to do this ? Thx : public class C<T> { private T myValue;
12
2740
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.
5
2921
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 an "AppleBasket" which is a class that contains a collection of apples. So, some code: class Fruit{ }
11
2498
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 provide an implemation of the operator +. Now I want to write another generic class Plan<DType>, which can
1
2438
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 "ThrowInConstructor" and "ThrowInFoo". First one throws "MyOwnException" in constructor, second one in "Foo()" method. There is a "GenericCatch" generics class able to accept "ThrowInConstructor" and "ThrowInFoo" as type parameter "<T>". There are two methods in...
2
1089
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 someone please help me understand why i would use a delegate in day to day use, over a function or sub? Thank you for your time! Ron
13
3830
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 application that needs implementations in both Java and C#. I have the Java side done, and it works fantastic, and the C# side is nearly there. The problem I'm running into has to do with the differences in implementations of Generics between the two...
0
8913
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8761
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
9200
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
9142
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8144
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6722
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6016
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4525
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3238
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

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.