Filling an Array

Posted on 2003-03-13
Medium Priority
Last Modified: 2012-08-13
I have some values that I would like to add to a DataTable.
There are multiples rows of data that I would like to fill a dataset using the loaddatarow method. this method takes an array object as input. the standard declaration for an array is

Dim myArray() as object={1,"Hello",2,0,5)
Dim dt as datatable


that works just fine. My question is I have about 20 rows to add to the datatable. What is the shortest way to add them to the array and then to the dataset?

The fastest way I could figure was continue to declare more array variables
Dim myArray1
Dim myArray2..ets

but there has got to be a better way to bulk load the data into the array.

your comments are appreciated

Question by:rvindust
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
LVL 28

Expert Comment

ID: 8133425
When adding a large number of rows you can optimize the performance by using the LoadDataRow
method (which takes an array of the values to be assigned) and bracketing your code in DataTable's
BeginLoadData and EndLoadData methods.
These methods temporarily disable and then reenable  notifications, index maintenance and constraints
while loading data)

Just imagine that you have your data in the text delimited file (myData.dat) structured as follows


Here is how to do it efficient

        ' Open the file, read its contents.
        Dim sr As New System.IO.StreamReader("myData.dat")
        Dim fileText As String = sr.ReadToEnd

        ' This regular expression defines a row of elements, and assigns a name
        ' to each group (that is, a field in the text row).
        Dim re As New System.Text.RegularExpressions.Regex("""(?<number1>[\d]+)"";""(?<mystring>[^""]+)"";(?<number2>[\d;]+);""(?<number3>[\d]+)"";""(?<number4>[\d]+)""")
        Dim ma As System.Text.RegularExpressions.Match

'turn off index maintenance and constraints
        For Each ma In re.Matches(fileText)
            ' A new line has been found, so add a row to the table.
            ' Create an array of values and add it in one operation
            Dim myArray() As Object = {ma.Groups("number1").Value, ma.Groups("mystring").Value, _
                ma.Groups("number2").Value, ma.Groups("number3").Value, ma.Groups("number4").Value}
            dt.LoadDataRow(myArray, True)
'turn on index maintenance and constraints

i might make a mistake with regular expressions, but you've got the idea

Author Comment

ID: 8138414
This is not a file read but hard coded values.
LVL 28

Expert Comment

ID: 8138523
Yoy can hardcode these values to your array. Approach is the same
Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.


Author Comment

ID: 8147553
That is what I am asking. what is the best way to hard code them into the array.
LVL 28

Accepted Solution

iboutchkine earned 200 total points
ID: 8147972
VB.NET support array which element of each is an array - known as jagged arrays

   ' this procedure tests arrays of arrays

    Sub TestJaggedArray()
        ' Initialize an array of arrays.
        Dim arr()() As String = {New String() {"a00"}, _
            New String() {"a10", "a11"}, _
            New String() {"a20", "a21", "a22"}, _
            New String() {"a30", "a31", "a32", "a33"}}

        ' Show how you can reference an element.
        Console.WriteLine(arr(3)(1))                    ' => a31

        ' Assign an entire row of elements.
        arr(0) = New String() {"a00", "a01", "a02"}

        ' Read an element just added.
        Console.WriteLine(arr(0)(2))                    ' => a02

        ' Expand one of the elements.
        ReDim Preserve arr(1)(3)
        ' Assign the new elements (now Nothing)
        arr(1)(2) = "a12"
        arr(1)(3) = "a13"
        ' Read back one of them
        Console.WriteLine(arr(1)(2))                    ' => a12
    End Sub

Author Comment

ID: 8156294
So to reference row 0, array 0;
It would look like... arr(0)

Author Comment

ID: 8156401
So to reference row 0, array 0;
It would look like... arr(0)

Author Comment

ID: 8156424
Awesome. Thanks for your help. Just one change I had to make that is to declare the array as an object not a string.

Featured Post

Create CentOS 7 Newton Packstack Running Keystone

A bug was filed against RDO for the installation of Keystone v3. This guide is designed to walk you through the configuration for using Keystone v3 with Packstack. You will accomplish this using various repos and the Answers file.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It seems a simple enough task, yet I see repeated questions asking how to do it: how to pass data between two forms. In this article, I will show you the different mechanisms available for you to do just that. This article is directed towards the .N…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Suggested Courses

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question