Home > Blockchain >  2d array of generic length in a class
2d array of generic length in a class

Time:10-06

I have a class that currently works like this.

Class Maze
    Public board(10, 10) As Object 
Dim maze1 As New Maze With {.board = {{" ", " ", "X", "X", " ", "X", "X", "X", " ", "X"},
                                      {"X", " ", " ", " ", " ", "X", " ", " ", " ", "X"},
                                      {" ", " ", "X", " ", "X", " ", " ", " ", " ", "X"},
                                      {"X", " ", "X", " ", " ", "X", "X", "X", " ", " "},
                                      {"X", " ", "X", "X", " ", " ", " ", " ", "X", " "},
                                      {" ", "X", " ", " ", "X", " ", " ", "X", " ", " "},
                                      {" ", "X", "X", " ", "X", " ", " ", " ", " ", "X"},
                                      {" ", " ", " ", " ", " ", "X", "X", " ", " ", "X"},
                                      {"X", "X", " ", "X", " ", " ", " ", "X", " ", " "},
                                      {" ", " ", " ", "X", "X", " ", " ", " ", "X", " "}}}

However I cant find away to remove the prespecified length without methods such as GetLength now not working.

CodePudding user response:

In the .Net world, if you define a type as Object you're almost always doing something very wrong you will soon come to regret. In this case, it looks like String may be more appropriate. However, perhaps based on how this is used later it might turn out that Integer or a specific class type could be better, alongside a special method to translate this into text or graphics for output.

Moving on to the specific issue of size... consider a List:

Class Maze
        Dim board As List(Of List(Of String))
        

board = New List(Of List(Of string)) From {
            New List(Of String) From {" ", " ", "X", "X", " ", "X", "X", "X", " ", "X"},
            New List(Of String) From {"X", " ", " ", " ", " ", "X", " ", " ", " ", "X"},
            New List(Of String) From {" ", " ", "X", " ", "X", " ", " ", " ", " ", "X"},
            New List(Of String) From {"X", " ", "X", " ", " ", "X", "X", "X", " ", " "},
            New List(Of String) From {"X", " ", "X", "X", " ", " ", " ", " ", "X", " "},
            New List(Of String) From {" ", "X", " ", " ", "X", " ", " ", "X", " ", " "},
            New List(Of String) From {" ", "X", "X", " ", "X", " ", " ", " ", " ", "X"},
            New List(Of String) From {" ", " ", " ", " ", " ", "X", "X", " ", " ", "X"},
            New List(Of String) From {"X", "X", " ", "X", " ", " ", " ", "X", " ", " "},
            New List(Of String) From {" ", " ", " ", "X", "X", " ", " ", " ", "X", " "}
            }

Now you can add a line to end like so:

board.Add(New List(Of String) From {" ", "X", " ", " ", " " ... })

Or add a column like this:

For Each c As List(Of String) In board
    c.Add(" ")
Next
  • Related