|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
A question about genericsBack 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 Showjumper wrote:
Show quoteHide quote > Hi Ashok,> 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 > 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 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... Show quoteHide quote "David Hogue" <davehogue+n***@gmail.com> wrote in message news:6Zkpg.12312$wA1.8101@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 > 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 -- Show quoteHide quoteHope 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:OBgjCBInGHA.4348@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 | | |
Close all forms but Main
converting string to image..?? Visual Basic .NET Compiler ClickOnce How to find equal images? amortization... kinda how to create shift-F2 function for Textbox - shortcutkey? VB.NET 2.0 & Application Settings.. VB Express: Can't Add New Icon File Discover SQL Servers on network |
|||||||||||||||||||||||