How to access a Dictionary with two-dimensional arrays in VB.NET

1 Answer

0 votes
Imports System
Imports System.Collections.Generic

Public Class Program
	Public Shared Sub Main()
        Dim dict As Dictionary(Of String, Integer(,)) = New Dictionary(Of String, Integer(,))()
        dict("matrix1") = New Integer(,) {
        {1, 2},
        {3, 4}}
        dict("matrix2") = New Integer(,) {
        {5, 6},
        {7, 8}}
		
        Dim matrix1 As Integer(,) = dict("matrix1")
        Dim matrix2 As Integer(,) = dict("matrix2")

        For i As Integer = 0 To matrix1.GetLength(0) - 1
            For j As Integer = 0 To matrix1.GetLength(1) - 1
                Console.Write(matrix1(i, j) & " ")
            Next

            Console.WriteLine()
        Next

        Dim value As Integer = dict("matrix2")(1, 1)
        Console.WriteLine("Element at [1,1] in matrix2 = " & value)
    End Sub
End Class


' run:
'
' 1 2 
' 3 4 
' Element at [1,1] in matrix2 = 8
'

 



answered Jan 21, 2025 by avibootz

Related questions

1 answer 74 views
1 answer 123 views
1 answer 82 views
1 answer 93 views
...