A question about generics

S

Showjumper

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
 
D

David Hogue

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

Showjumper

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

Jay B. Harlow [MVP - Outlook]

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/library/system.collections.objectmodel.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
|
| 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
|
|
|
 

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