Tim the Enchanter
Tim the Enchanter

Reputation:

VB.net list/array of objects

Please be gentle,

I am essentially trying to create a list of records in VB.net using the 1.1 framework.

It seems that I should be using an object even though I have only properties but no methods (more like a traditional record), I would like to store a list of objects, how can I create the list and add an instance of the object to the list.

I do have sample code that almost works but it is too poor for public display.

Upvotes: 0

Views: 2809

Answers (3)

Spencer Ruport
Spencer Ruport

Reputation: 35107

The .Net framework has it's own lists built in. Are you trying to write your own for a reason?

A list class in any language is just going to be an array with some methods to improve performance and to abstract some of the gritty details.

Upvotes: 0

John Saunders
John Saunders

Reputation: 161773

Are these database records? If so, then when you fill a DataSet, you get a DataTable containing a list of Rows.

If not, then consider the use of the ArrayList class. Alternatively, if you will need to access the records by key later on, look at the HashTable class.

Upvotes: 0

Fredou
Fredou

Reputation: 20100

are you looking for the arraylist?

Imports System
Imports System.Collections
Imports Microsoft.VisualBasic

Public Class SamplesArrayList    

Public Shared Sub Main()

    ' Creates and initializes a new ArrayList.
    Dim myAL As New ArrayList()
    myAL.Add("Hello")
    myAL.Add("World")
    myAL.Add("!")

    ' Displays the properties and values of the ArrayList.
    Console.WriteLine("myAL")
    Console.WriteLine("    Count:    {0}", myAL.Count)
    Console.WriteLine("    Capacity: {0}", myAL.Capacity)
    Console.Write("    Values:")
    PrintValues(myAL)
End Sub

Public Shared Sub PrintValues(myList As IEnumerable)
    Dim obj As [Object]
    For Each obj In  myList
        Console.Write("   {0}", obj)
    Next obj
    Console.WriteLine()
End Sub 'PrintValues

End Class


' This code produces output similar to the following:
' 
' myAL
'     Count:    3
'     Capacity: 4
'     Values:   Hello   World   !

Upvotes: 1

Related Questions