นำมาฝาก : ตัวอย่างการใช้งาน ArrayList
Creating Type- Strong List Using VB.Net
Step 1: Create your own Class like Movie Step 2: Create Collection Class Inherit from CollectionBase Step 3: In that Collection Class Item Method is must Step 4: Create instance of the own Class Step 5: Add all instance to the CollectionBase Class
Sample:
Imports System Imports System.Collections
public class Movie Public MovieName as String public Actor as string public Rating as String End class
public Class MovieCollection Inherits CollectionBase public Sub Add(_movie as Movie) List.Add(_movie) End Sub
Default public overridable readonly Property Item(index as Integer)as Movie Get Return CType(Me.List(index),Movie) end Get End Property End Class
public Module ModMain Sub Main() Dim Movies as MovieCollection = New MovieCollection() Dim RMovie as Movie Dim Index as Integer
'Index = 0 RMovie = new Movie() RMovie.MovieName = "The Transporter 2" RMovie.Actor = "Jason Statham" RMovie.Rating = "PG" Movies.Add(RMovie)
'Index = 1 RMovie = new Movie() RMovie.MovieName = "A Sound of Thunder" RMovie.Actor = "Ben Kingsley" RMovie.Rating = "PG" Movies.Add(RMovie)
For Index = 0 to Movies.Count - 1 Console.WriteLine("--------") Console.WriteLine(Index) Console.WriteLine("--------") Console.WriteLine("Movie Name :- " & Movies(Index).MovieName) Console.WriteLine("Actor Name :- " & Movies(Index).Actor) Console.WriteLine("Rating :- " & Movies(Index).Rating) next End sub End Module
|