473,320 Members | 2,146 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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
articlescollections 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 2577
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
articlescollections 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 ArticlesCollection class you can inherit from List and
specify a type:

Public Class ArticlesCollection
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 ArticlesCollection where
articlescollection 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
articlescollections 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 ArticlesCollection class you can inherit from List and
specify a type:

Public Class ArticlesCollection
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.Collections.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 ArticleCollection
Inherits System.Collections.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 DomainCollection(Of T)
Inherits System.Collections.ObjectModel.Collection(Of T)

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

End Class

Public Class ArticleCollection
Inherits DomainCollection(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**************@TK2MSFTNGP02.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
| articlescollections 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
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...
27
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...
11
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...
12
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...
11
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...
9
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...
1
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...
11
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>>...
8
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...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.