473,757 Members | 2,081 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

A question about generics


Back in asp.net 1.1 i made custom collection classes per Karl Seguin's
article On the Way to Mastering ASP.NET: Introducing Custom Entity Classes
to take advantage of strongly typed data. Now with generics i understand
that one doesnt have to used these custom collections. So i want to move
this 1.1 project to 2.0. I have been reading about generics but i geuss i
dont really get hpw to implement them. For example i have 2 classees in the
1.1 projecy - Articles which exposes the various properties and the custom
articlescollect ions that has the various methods remove, indexof, copyto
etc. How do i make the move to generics and still have my strongly typed
data? I assum i would still have the articles class but would only have a
single generic collection class that could accept varous types?

Thanks
Ashok

Jun 30 '06 #1
3 2605
Showjumper wrote:

Back in asp.net 1.1 i made custom collection classes per Karl Seguin's
article On the Way to Mastering ASP.NET: Introducing Custom Entity Classes
to take advantage of strongly typed data. Now with generics i understand
that one doesnt have to used these custom collections. So i want to move
this 1.1 project to 2.0. I have been reading about generics but i geuss i
dont really get hpw to implement them. For example i have 2 classees in the
1.1 projecy - Articles which exposes the various properties and the custom
articlescollect ions that has the various methods remove, indexof, copyto
etc. How do i make the move to generics and still have my strongly typed
data? I assum i would still have the articles class but would only have a
single generic collection class that could accept varous types?

Thanks
Ashok


Hi Ashok,

In most cases I have found List or Dictionary work well. List is
similar to ArrayList and Dictionary is similar to Hashtable. For your
Articles you could use a List like this:

Dim foo As List(Of Article)

You get strongly typed data because only articles can be put in the list
and you don't need to cast when you get an item out of the list. If you
still want an ArticlesCollect ion class you can inherit from List and
specify a type:

Public Class ArticlesCollect ion
Inherits List(Of Article)

End Class
--
David Hogue
Jul 1 '06 #2

I think i have made some headway in understanding how to use generics.
Previously i had Function GetAllArticles as ArticlesCollect ion where
articlescollect ion was the ucstom collections class. Now i have done Public
Function GetAllArticles( ) As IEnumerable(Of Articles) and this seems to work
fine. I assume this correct? Thanks for your time...
"David Hogue" <da************ @gmail.com> wrote in message
news:6Z******** **********@fe03 .news.easynews. com...
Showjumper wrote:

Back in asp.net 1.1 i made custom collection classes per Karl Seguin's
article On the Way to Mastering ASP.NET: Introducing Custom Entity
Classes
to take advantage of strongly typed data. Now with generics i understand
that one doesnt have to used these custom collections. So i want to move
this 1.1 project to 2.0. I have been reading about generics but i geuss i
dont really get hpw to implement them. For example i have 2 classees in
the
1.1 projecy - Articles which exposes the various properties and the
custom
articlescollect ions that has the various methods remove, indexof, copyto
etc. How do i make the move to generics and still have my strongly typed
data? I assum i would still have the articles class but would only have a
single generic collection class that could accept varous types?

Thanks
Ashok


Hi Ashok,

In most cases I have found List or Dictionary work well. List is
similar to ArrayList and Dictionary is similar to Hashtable. For your
Articles you could use a List like this:

Dim foo As List(Of Article)

You get strongly typed data because only articles can be put in the list
and you don't need to cast when you get an item out of the list. If you
still want an ArticlesCollect ion class you can inherit from List and
specify a type:

Public Class ArticlesCollect ion
Inherits List(Of Article)

End Class
--
David Hogue


Jul 1 '06 #3
Ashok,
In addition to the other comments.

I normally use the collection classes in System.Collecti ons.ObjectModel to
define my strongly typed collections. I reserve List(Of T) & Dictionary(Of
T) for implementation details and generally don't inherit directly from
them.

For example:

Public Class ArticleCollecti on
Inherits System.Collecti ons.ObjectModel .Collection(Of Article)

End Class

Will produce a fully typed safe collection including remove, indexof, copyto
etc...

http://msdn2.microsoft.com/en-us/lib...jectmodel.aspx

I will inherited from Collection(Of T) to create a custom base class that
will add additional common functionality to my strongly typed collections.

For example:

Public MustInherit Class DomainCollectio n(Of T)
Inherits System.Collecti ons.ObjectModel .Collection(Of T)

Public Function Find(ByVal match As Predicate(Of T)) As T
Dim list As List(Of T) = TryCast(Me.Item s, List(Of T))
If list Is Nothing Then Return Nothing
Return list.Find(match )
End Function

End Class

Public Class ArticleCollecti on
Inherits DomainCollectio n(Of Article)

End Class
--
Hope this helps
Jay B. Harlow [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net
"Showjumper " <dfgkjhdf> wrote in message
news:OB******** ******@TK2MSFTN GP02.phx.gbl...
|
| Back in asp.net 1.1 i made custom collection classes per Karl Seguin's
| article On the Way to Mastering ASP.NET: Introducing Custom Entity Classes
| to take advantage of strongly typed data. Now with generics i understand
| that one doesnt have to used these custom collections. So i want to move
| this 1.1 project to 2.0. I have been reading about generics but i geuss i
| dont really get hpw to implement them. For example i have 2 classees in
the
| 1.1 projecy - Articles which exposes the various properties and the custom
| articlescollect ions that has the various methods remove, indexof, copyto
| etc. How do i make the move to generics and still have my strongly typed
| data? I assum i would still have the articles class but would only have a
| single generic collection class that could accept varous types?
|
| Thanks
| Ashok
|
|
|
Jul 1 '06 #4

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

Similar topics

2
2744
by: Wiktor Zychla | last post by:
I've read several documents about upcoming C# generics and I still cannot understand one thing. Would it be valid to write a code like this: class SomeClass { public void AMethod<T>(T a, T b) { T c = a + b; // ?
27
2461
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!
11
1230
by: John Salerno | last post by:
I'm thinking about reading Beginning C# Objects: From Concepts to Code because I still don't have a great grasp of objects, but I wonder if C# 2.0 will change things enough that a lot of what's in the book will no longer be relevant or applicable? I know generics are the big change, but since I don't have much of a concept of what they are yet, I don't know how much they (and other changes) will effect any of the content of books released...
12
2744
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.
11
2501
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
9
5985
by: sloan | last post by:
I'm not the sharpest knife in the drawer, but not a dummy either. I'm looking for a good book which goes over Generics in great detail. and to have as a reference book on my shelf. Personal Experience Only, Please. ...
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...
11
2482
by: hammad.awan_nospam | last post by:
Hello, I'm wondering if it's possible to do the following with Generics: Let's say I have a generic member variable as part of a generic class like this: List<DLinqQuery<TDataContext>> _queries; where DLinqQuery is a generic class that takes a type parameter
8
1471
by: Tony Johansson | last post by:
Hello! I have read that in practice, casting proved to be several times faster than using a generic. So the main reason to use generics is not that the performance is better because that's not the case. The only reason is that it's type-safe. I must ask if anyone has made any significant performance improvement using
0
9297
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,...
0
10069
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9884
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
9735
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
8736
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
7285
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
6556
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
5168
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...
3
3395
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.