fedeteka
fedeteka

Reputation: 963

3D array loop and get the first dimension value

A 3D array like this

Dim MyFonts = 
    {
        {"Arial", "arial.ttf", "arialbd.ttf"}, 
        {"Calibri", "calibri.ttf", "calibribd.ttf"},
        {"Candara", "Candara.ttf", "Candarab.ttf"}, 
        {"Comic Sans MS", "comic.ttf", "comicbd.ttf"}, 
        {"Consolas", "consola.ttf", "consolab.ttf"},
        {"Constantia", "constant.ttf", "constantb.ttf"},
        {"Courier New","cour.ttf", "courbd.ttf"},
        {"Georgia", "georgia.ttf", "georgiab.ttf"},
        {"Impact", "impact.ttf", "impact.ttf"},
        {"Palatino Linotype", "pala.ttf", "palab,ttf"},
        {"Tahoma", "tahoma.ttf", "tahomabd.ttf"},
        {"Times New Roman", "times.ttf", "timesbd.ttf"},
        {"Trebuchet MS", "trebuc.ttf", "trebucbd.ttf"},
        {"Verdana", "verdana.ttf", "verdanab.ttf"}
    }

Where the first dimension is the font name, the second is the True Type Font file for the normal style, and the third dimension is the True Type Font file for the bold style

I want to populate a combobox with the font names (first dimension)

For index0 = 0 To MyFonts.GetUpperBound(0)
    'Add all the Fonts names to a Combobox
    myCombobox.Items.Add(MyFonts(index0))
Next

I get the error: "Number of indexes is less than the number of dimensions of the indexed array"

Even if I use

myCombobox.Items.Add(MyFonts(index0,,))

Upvotes: 1

Views: 74

Answers (2)

djv
djv

Reputation: 15774

You have a 2D array. The array is indexed like this

enter image description here

and when indexed, the value returned is a string.

Consider the following array, which is 3D

enter image description here

Your original information is really only 1D because each element corresponds to a single font. Now, if you had duplicates of the same font i.e. Arial twice, but with different ttf files, then you had two logical dimensions of information.

You can test these different dimensions with the following code. The indexers are at the end

Sub Main()
    Dim MyFonts2D =
    {
        {"Arial", "arial.ttf", "arialbd.ttf"},
        {"Calibri", "calibri.ttf", "calibribd.ttf"},
        {"Candara", "Candara.ttf", "Candarab.ttf"},
        {"Comic Sans MS", "comic.ttf", "comicbd.ttf"},
        {"Consolas", "consola.ttf", "consolab.ttf"},
        {"Constantia", "constant.ttf", "constantb.ttf"},
        {"Courier New", "cour.ttf", "courbd.ttf"},
        {"Georgia", "georgia.ttf", "georgiab.ttf"},
        {"Impact", "impact.ttf", "impact.ttf"},
        {"Palatino Linotype", "pala.ttf", "palab,ttf"},
        {"Tahoma", "tahoma.ttf", "tahomabd.ttf"},
        {"Times New Roman", "times.ttf", "timesbd.ttf"},
        {"Trebuchet MS", "trebuc.ttf", "trebucbd.ttf"},
        {"Verdana", "verdana.ttf", "verdanab.ttf"}
    }

    Dim MyFonts3D =
    {
        {
            {"Arial", "arial1.ttf", "arialbd1.ttf"},
            {"Arial", "arial2.ttf", "arialbd2.ttf"},
            {"Arial", "arial3.ttf", "arialbd3.ttf"}
        },
        {
            {"Calibri", "Calibri1.ttf", "Calibribd1.ttf"},
            {"Calibri", "Calibri2.ttf", "Calibribd2.ttf"},
            {"Calibri", "Calibri3.ttf", "Calibribd3.ttf"}
        },
        {
            {"Candara", "Candara1.ttf", "Candarabd1.ttf"},
            {"Candara", "Candara2.ttf", "Candarabd2.ttf"},
            {"Candara", "Candara3.ttf", "Candarabd3.ttf"}
        }
    }

    Console.WriteLine("Enter first index")
    Dim i = Integer.Parse(Console.ReadLine())
    Console.WriteLine("Enter second index")
    Dim j = Integer.Parse(Console.ReadLine())
    Console.WriteLine("Enter third index")
    Dim k = Integer.Parse(Console.ReadLine())
    Dim a = MyFonts2D(i, j)
    Console.WriteLine(a)
    Dim b = MyFonts3D(i, j, k)
    Console.WriteLine(b)
    Console.ReadLine()
End Sub

Upvotes: 4

A class will make it easy to display the name, but allow you to get the related TTF or bold file from whatever they select:

Public Class FontItem
    Public Property Name As String
    Public Property TTFile As String
    Public Property TTBoldFile As String
    Public Sub New(n As String, f As String, b As String)
        Name = n
        TTFile = f
        TTBoldFile = b
    End Sub

    Public Overrides Function ToString() As String
        Return Name
    End Function
End Class

This "ties" the name with the 2 files to make it very easy to get the related file. Next, create a list of those things from the data you have:

Dim myFonts As New List(Of FontItem)

Dim data = {{"Arial", "arial.ttf", "arialbd.ttf"},
             ...your long list
           }

For n As Int32 = 0 To data.GetUpperBound(0)
    myFonts.Add(New FontItem(data(n, 0), data(n, 1), data(n, (2))))
Next

cbox1.DataSource = myFonts

I would have built that data differently, but this allows you to use what you have. There is no need to copy to the data into the control, the SelectedItem will be a FontItem (inside an Object). In the SelectedValueChanged event:

Dim item = DirectCast(cbox1.SelectedValue, FontItem)
Console.WriteLine("For {0}, TTF = {1}, bold = {2}", item.Name,
                        item.TTFile,
                        item.TTBoldFile)

enter image description hereenter image description here

Upvotes: 5

Related Questions