-
Jan 8th, 2009, 11:47 AM
#1
Thread Starter
Junior Member
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
-
Jan 8th, 2009, 11:50 AM
#2
Re: Array of Constants
Do you mean fixed size arrays, or do you mean arrays of constant values?
My usual boring signature: Nothing
-
Jan 8th, 2009, 11:51 AM
#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}}
-
Jan 8th, 2009, 11:53 AM
#4
Hyperactive Member
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)
Did I help you with your problem? If I did rate me by clicking here: Rate knxrb
-
Jan 8th, 2009, 12:03 PM
#5
Thread Starter
Junior Member
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...
-
Jan 8th, 2009, 12:06 PM
#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.
-
Jan 8th, 2009, 12:13 PM
#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:is a 2D array. This: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.
-
Jan 8th, 2009, 12:23 PM
#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.
My usual boring signature: Nothing
-
Jan 9th, 2009, 06:54 AM
#9
Thread Starter
Junior Member
Re: Array of Constants
Hello, and thanks for the help you've been giving me
i've another problem, when i try this
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"}
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...
-
Jan 9th, 2009, 08:29 AM
#10
Re: Array of Constants
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.
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: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.
-
Jan 9th, 2009, 10:45 AM
#11
Thread Starter
Junior Member
Re: Array of Constants
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"}
Then all i need to do is...
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"}
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|