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
Printable View
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
Do you mean fixed size arrays, or do you mean arrays of constant values?
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}}
What does "bidimensional" mean?
If you want to create arrays you can do this:
And then to set values you can do this:Code:Dim strArray(100) As String
Dim dbArray(300) As Double
And to get the values you can do this:Code:strArray(0) = "String 1"
strArray(1) = "String 2"
etc...
Code:MessageBox.Show(strArray(0))
Dim str2 As String = strArray(1)
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...
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.
As Atheist says, if N and M are different then you're creating a jagged array, in which case your notation is incorrect. This: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.vb.net Code:
Dim Zone as String()()
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.
Hello, and thanks for the help you've been giving me
i've another problem, when i try this
it gives the following errorCode:Dim zona()() As String
Form_Load...
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"}
how can i bypass this, since the program should already be running...Code:NullReferenceException was Unhandeled
Object reference not set to an instance of an object.
As I said, a jagged array is an array of arrays. This line: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.vb.net Code:
Dim zona()() As String
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:This line creates a jagged array where the outer array can itself contain 10 arrays:vb.net Code:
Dim arr()() As IntegerSo does this:vb.net Code:
Dim arr(9)() As Integerand this:vb.net Code:
Dim arr()() As Integer ReDim arr(9)Once you've done that, THEN you can assign the inner 1D arrays to the elements of the outer array.vb.net Code:
Dim arr()() As Integer = New Integer(9)() {}
Then all i need to do is...Code:Dim zona()() As String
Form_Load...
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?Code:Dim zona(1)() As String
Form_Load...
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"}