Error populating array

G

Guest

Can someone please help me out?

I have a dataset that contains 1 column.
I'm trying to populate an array from this column.
I'm using the code pasted below, and keep getting the following error on the
4th line of code:

Object reference not set to an instance of an object.

Dim arr() As String
Dim j As Integer = 0
For Each row As DataRow In _ds2.Tables(0).Rows

arr(j) = row(0).ToString
j = j + 1

Next

TIA,
Amber
 
R

Roel Oost

For Each row As DataRow In _ds2.Tables(0).Rows
Redim Preserve arr(j)
arr(j) = row(0).ToString
j = j + 1
Next

But why not using an arraylist

Dim l_arr as ArrayList
l_arr=New ArrayList(_ds2.Tables(0).Rows.Count)
'l_arr=New ArrayList() is oke too, your arraylist shall then dynamicly be
resized
For Each row As DataRow In _ds2.Tables(0).Rows
l_arr.Add(row(0).ToString)
Next
 
G

Guest

Instead of using ReDim Preserve on every iteration, just use ReDim once prior
to entering the loop - it's much faster. (unless you decide to use ArrayList
of course).
--
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB.NET to C# Converter
Instant VB: C# to VB.NET Converter
Instant J#: VB.NET to J# Converter
Clear VB: Cleans up outdated VB.NET code
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top