1. ## Array of Constants

Mi question is the following

i've to declare two constant arrays
one bidemensional of strings
the other bidimensional of Doubles

Is there any possibility to do this? How can i do this?

Thanks
Carlos

2. ## Re: Array of Constants

Do you mean fixed size arrays, or do you mean arrays of constant values?

3. ## Re: Array of Constants

No, it's not possible. Array elements are inherently variable. That said, if you don't change the values of the elements once they're created then their values will remain constant, even though they CAN vary. To initialise a 2D array you would do like this:
vb.net Code:
`Dim arr As Integer(,) = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}`

4. ## Re: Array of Constants

What does "bidimensional" mean?

If you want to create arrays you can do this:

Code:
```Dim strArray(100) As String
Dim dbArray(300) As Double```
And then to set values you can do this:
Code:
```strArray(0) = "String 1"
strArray(1) = "String 2"
etc...```
And to get the values you can do this:
Code:
```MessageBox.Show(strArray(0))
Dim str2 As String = strArray(1)```

5. ## Re: Array of Constants

I meant arrays of constants

by bidimensional i mean 2D array

can i do this?

Dim Zone as String(,) = {{"zone1", "zone2", ..., "zoneN"},{"zone1", "zone2", ..., "zoneM"}}

and N, M are diferent or do they need to be equal...

6. ## Re: Array of Constants

It still wont be an array of constants really.

Yes you can do like you've showed. If N and M are not equal, it'll be what is called a "jagged" array.

7. ## Re: Array of Constants

As Atheist says, if N and M are different then you're creating a jagged array, in which case your notation is incorrect. This:
vb.net Code:
`Dim Zone as String(,)`
is a 2D array. This:
vb.net Code:
`Dim Zone as String()()`
is a jagged array. A multidimensional array is a single array object with multiple dimensions, while a jagged array is a 1-dimensional array of 1-dimensional arrays.

8. ## Re: Array of Constants

Why do you need them to be constant? If this is something that others will be using, and you don't want them to have the ability to alter the values in the arrays, then I would suggest wrapping the arrays in a class, and providing access to the members via properties, such that you can simply make the properties read only, which will block the ability to change the values.

9. ## Re: Array of Constants

i've another problem, when i try this

Code:
```Dim zona()() As String

zona(0) = New String() {"Almada", "Amadora", "Aveiro", "Braga", "Cascais", "Coimbra", "Faro", "Guimarães", "Leiria", "Lisboa", "Matosinhos", "Oeiras", "Porto", "Póvoa de Varzim", "Setúbal", "Sintra", "Vila Nova de Gaia"}
zona(1) = New String() {"Albufeira", "Angra do Heroísmo", "Barcelos", "Beja", "Bragança", "Cartaxo", "Castelo Branco", "Covilhã", "Elvas", "Évora", "Figueira da Foz", "Funchal", "Guarda", "Lamego", "Loulé", "Loures", "Mafra", "Maia", "Marinha Grande", "Montijo", "Odivelas", "Ovar", "Paços de Ferreira", "Palmela", "Penafiel", "Ponta Delgada", "Portalegre", "Portimão", "Santa Maria da Feira", "Santarém", "Santiago do Cacém", "Seixal", "Sesimbra", "Sines", "Tavira", "Tomar", "Valongo", "Viana do Castelo", "Vila do Conde", "Vila Real", "Viseu"}```
it gives the following error
Code:
```NullReferenceException was Unhandeled
Object reference not set to an instance of an object.```
how can i bypass this, since the program should already be running...

10. ## Re: Array of Constants

As I said, a jagged array is an array of arrays. This line:
vb.net Code:
`Dim zona()() As String`
Declares a variable that can refer to such an array, but it doesn't actually create the array itself. The next two lines are trying to assign the inner arrays to the elements of the outer array, but you haven't actually created the outer array yet.

You cannot create a jagged array all in one go like you can a multidimensional array. A Multidimensional array is a single array object, while a jagged array an outer 1D array object where each element is a 1D array object. To create an jagged array you must do it in two steps. This line declares a jagged array variable but does not create an array object:
vb.net Code:
`Dim arr()() As Integer`
This line creates a jagged array where the outer array can itself contain 10 arrays:
vb.net Code:
`Dim arr(9)() As Integer`
So does this:
vb.net Code:
`Dim arr()() As Integer ReDim arr(9)`
and this:
vb.net Code:
`Dim arr()() As Integer = New Integer(9)() {}`
Once you've done that, THEN you can assign the inner 1D arrays to the elements of the outer array.

11. ## Re: Array of Constants

Code:
```Dim zona()() As String

zona(0) = New String() {"Almada", "Amadora", "Aveiro", "Braga", "Cascais", "Coimbra", "Faro", "Guimarães", "Leiria", "Lisboa", "Matosinhos", "Oeiras", "Porto", "Póvoa de Varzim", "Setúbal", "Sintra", "Vila Nova de Gaia"}
zona(1) = New String() {"Albufeira", "Angra do Heroísmo", "Barcelos", "Beja", "Bragança", "Cartaxo", "Castelo Branco", "Covilhã", "Elvas", "Évora", "Figueira da Foz", "Funchal", "Guarda", "Lamego", "Loulé", "Loures", "Mafra", "Maia", "Marinha Grande", "Montijo", "Odivelas", "Ovar", "Paços de Ferreira", "Palmela", "Penafiel", "Ponta Delgada", "Portalegre", "Portimão", "Santa Maria da Feira", "Santarém", "Santiago do Cacém", "Seixal", "Sesimbra", "Sines", "Tavira", "Tomar", "Valongo", "Viana do Castelo", "Vila do Conde", "Vila Real", "Viseu"}```
Then all i need to do is...

Code:
```Dim zona(1)() As String

zona(0) = New String() {"Almada", "Amadora", "Aveiro", "Braga", "Cascais", "Coimbra", "Faro", "Guimarães", "Leiria", "Lisboa", "Matosinhos", "Oeiras", "Porto", "Póvoa de Varzim", "Setúbal", "Sintra", "Vila Nova de Gaia"}
zona(1) = New String() {"Albufeira", "Angra do Heroísmo", "Barcelos", "Beja", "Bragança", "Cartaxo", "Castelo Branco", "Covilhã", "Elvas", "Évora", "Figueira da Foz", "Funchal", "Guarda", "Lamego", "Loulé", "Loures", "Mafra", "Maia", "Marinha Grande", "Montijo", "Odivelas", "Ovar", "Paços de Ferreira", "Palmela", "Penafiel", "Ponta Delgada", "Portalegre", "Portimão", "Santa Maria da Feira", "Santarém", "Santiago do Cacém", "Seixal", "Sesimbra", "Sines", "Tavira", "Tomar", "Valongo", "Viana do Castelo", "Vila do Conde", "Vila Real", "Viseu"}```
Isn't it?

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•

Featured