Login or Sign Up to become a member!
LessThanDot Sit Logo

LessThanDot

Community Wiki

Less Than Dot is a community of passionate IT professionals and enthusiasts dedicated to sharing technical knowledge, experience, and assistance. Inside you will find reference materials, interesting technical discussions, and expert tips and commentary. Once you register for an account you will have immediate access to the forums and all past articles and commentaries.

LTD Social Sitings

Lessthandot twitter Lessthandot Linkedin Lessthandot friendfeed Lessthandot facebook Lessthandot rss

Note: Watch for social icons on posts by your favorite authors to follow their postings on these and other social sites.

Navigation

Google Ads

ASP.NET: Using Generics to create a property list

From Wiki

Jump to: navigation, search

Summary: An example of how the System.Collections.Generic namespace can allow us to create a Property which can be added to, referred to by index and also iterated.

I recently came across a situation where I wanted to add a list of objects to a class and then refer to them by index. The System.Collections.Generic namespace came to the rescue, specifically via it's List Generic class.

To demonstrate the situation, imagine we have a box which can contain multiple files. Firstly, I'd need to create a box class (perhaps with an ID so that we can reference it later) and then I'd need a Files property to which I could add multiple files. I'll also have to create a File class which I'll add an ID to as well.

To represent this in code, we would create a File class with one property for the ID e.g.

  1. Public Class File
  2.  
  3.     Sub New(ByVal ID As Integer)
  4.         _FileID = ID
  5.     End Sub
  6.  
  7.     Public Property FileID() As Integer
  8.         Get
  9.             Return _FileID
  10.         End Get
  11.         Set(ByVal value As Integer)
  12.             _FileID = value
  13.         End Set
  14.     End Property
  15.  
  16.     Private _FileID As Integer
  17.  
  18. End Class

Then, we need to look at how we create our Box class and this is where the Generics come in. Unlike our File class above, we need to be able to create a list of files rather than just one variable such as the ID. To do this, we can use the List class and the IList interface. When we create our Files property we need to tell it that it is actually a list and also what the list contains. In this case it will contain a list of our Files class e.g.

  1. Imports System.Collections.Generic  
  2.     Public Class Box
  3.  
  4.         Sub New(ByVal ID As Integer)
  5.             _BoxID = ID
  6.             _Files = New List(Of File)
  7.         End Sub
  8.  
  9.         Public Property BoxID() As Integer
  10.             Get
  11.                 Return _BoxID
  12.             End Get
  13.             Set(ByVal value As Integer)
  14.                 _BoxID = value
  15.             End Set
  16.         End Property
  17.  
  18.         Public Property Files() As IList(Of File)
  19.             Get
  20.                 Return _Files
  21.             End Get
  22.             Set(ByVal value As IList(Of File))
  23.                 _Files = value
  24.             End Set
  25.         End Property
  26.  
  27.         Private _BoxID As Integer
  28.         Private _Files As IList(Of File)
  29.  
  30.     End Class

Now, if I want to create a new box and add some files to it, I can do so very easily like this:

  1. ' Create a box  
  2. Dim b As New Box(1)
  3.  
  4. ' Add a few files  
  5. b.Files.Add(New File(1))  
  6. b.Files.Add(New File(2))  
  7. b.Files.Add(New File(3))

Also, if I need to go back and reference a file by it's index I can do so like this:

  1. ' Get a file by index  
  2. Dim myFile As File
  3. myFile = b.Files(2)

And, if needed, I can loop through the entire list of files:

  1. ' Loop through the collection of files  
  2. For Each f As File In b.Files  
  3.     Response.Write(f.FileID)  
  4. Next

This Hack is part of the ASP.NET Hacks collection

463 Rating: 2.6/5 (7 votes cast)