I need to define a two dimensional array and assign values. I was wondering if there is an easier way in vb.net to do this other then how I have it below. some of my other arrays have over 700 elements in them.

Dim L1(8, 2) As Double

L1(0, 0) = 0
L1(1, 0) = 6.2170397
L1(2, 0) = 3.055655
L1(3, 0) = 6.10455
L1(4, 0) = 2.83532
L1(5, 0) = 5.8268
L1(6, 0) = 2.5185
L1(7, 0) = 5.48
L1(0, 1) = 3.052
L1(1, 1) = 2.149
L1(2, 1) = 6.12
L1(3, 1) = 0.0
L1(4, 1) = 5.62
L1(5, 1) = 4.57
L1(6, 1) = 3.04
L1(7, 1) = 5.09

Dani AI

Generated

Two practical approaches: embed the numbers in code (compact, fastest at runtime) or keep them external and parse at startup (easier to maintain). A frequent VB pitfall worth noting up front: VB declarations use upper bounds, not lengths — for an 8-by-2 array declare Dim L1(7, 1) As Double. Declaring Dim L1(8, 2) creates a 9-by-3 array.

Inline initialiser (good for small, truly static tables):

Dim L As Double(,) = {{1.0, 2.0, 3.0, 4.0}, {5.0, 6.0, 7.0, 8.0}}

File-based parsing (good for the text-file workflow mentioned by and when sets change often):

Imports System.IO
Imports System.Globalization

Dim lines = File.ReadAllLines("data.csv")
Dim rows = lines.Length
Dim cols = lines(0).Split(","c).Length
Dim data(rows - 1, cols - 1) As Double

For r = 0 To rows - 1
    Dim parts = lines(r).Split(","c)
    For c = 0 To cols - 1
        Dim v As Double
        If Double.TryParse(parts(c).Trim(), NumberStyles.Float, CultureInfo.InvariantCulture, v) Then
            data(r, c) = v
        Else
            ' handle parse error or missing value
        End If
    Next
Next

Practical tips: for ~700 values an inline initializer is feasible but becomes hard to edit — store as CSV or an embedded resource for maintainability. Use Double.TryParse with InvariantCulture to avoid decimal-separator issues. If the data are pairs, consider an array of small structs/tuples for clarity. Use a database (as suggested) only when query/update functionality or concurrent access is required.

Recommended Answers

All 4 Replies

If the elements are stored in a database or Excel workbook, you can just pass it in with a loop.

Where are you getting your data from? (If you can disclose that information)

They are currently in a text file, I could import them into a excel spread sheet.

I was thinking there might be a way to define them like in c++

Double L1[8, 2]  = { 0, 
                 6.2170397,
                 3.055655,
                 6.10455,
                 2.83532,
                 5.8268,
                 2.5185,
                 5.48},
                {3.052,
                 2.149,
                 6.12,
                 0.0,
                 5.62, 
                 4.57,
                 3.04,
                 5.09}

but vb.net does not like that.

The easiest way I know of would be to loop through the file and add the values.
If they were comma delimited and in order, you could just loop until you hit end of file.

Ulpoad data from text file and create database for the same.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.