By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,985 Members | 2,920 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,985 IT Pros & Developers. It's quick & easy.

A question about generics

P: n/a

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
Share this Question
Share on Google+
3 Replies


P: n/a
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

P: n/a

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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.