Array of Constants-VBForums
Results 1 to 11 of 11

Thread: Array of Constants

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Nov 2008
    Posts
    21

    Exclamation 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. #2
    Loquacious User Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    24,399

    Re: Array of Constants

    Do you mean fixed size arrays, or do you mean arrays of constant values?
    My usual boring signature: Nothing

  3. #3
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    88,643

    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:
    1. Dim arr As Integer(,) = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}

    2007-2014

    Why is my data not saved to my database? | MSDN Data Walkthroughs
    MSDN "How Do I?" Videos: VB | C#
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts) | WP8 Turnstile Feather Transition with Pivot Control
    Beginner Tutorials: VB | C# | SQL

  4. #4
    Hyperactive Member knxrb's Avatar
    Join Date
    Jul 2007
    Location
    United Kingdom
    Posts
    321

    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

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Nov 2008
    Posts
    21

    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. #6
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    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.
    ---My Flickr photo (mostly screenshots these days!) stream. Have a look!

    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    TCP client/server connection | Retrieving the EventHandler for any Event by code.
    Me on.. BitBucket, Google Code, Github (pretty empty)

  7. #7
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    88,643

    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:
    1. Dim Zone as String(,)
    is a 2D array. This:
    vb.net Code:
    1. 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.

    2007-2014

    Why is my data not saved to my database? | MSDN Data Walkthroughs
    MSDN "How Do I?" Videos: VB | C#
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts) | WP8 Turnstile Feather Transition with Pivot Control
    Beginner Tutorials: VB | C# | SQL

  8. #8
    Loquacious User Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    24,399

    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

  9. #9

    Thread Starter
    Junior Member
    Join Date
    Nov 2008
    Posts
    21

    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...

  10. #10
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    88,643

    Re: Array of Constants

    As I said, a jagged array is an array of arrays. This line:
    vb.net Code:
    1. 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:
    1. Dim arr()() As Integer
    This line creates a jagged array where the outer array can itself contain 10 arrays:
    vb.net Code:
    1. Dim arr(9)() As Integer
    So does this:
    vb.net Code:
    1. Dim arr()() As Integer
    2.  
    3. ReDim arr(9)
    and this:
    vb.net Code:
    1. 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.

    2007-2014

    Why is my data not saved to my database? | MSDN Data Walkthroughs
    MSDN "How Do I?" Videos: VB | C#
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts) | WP8 Turnstile Feather Transition with Pivot Control
    Beginner Tutorials: VB | C# | SQL

  11. #11

    Thread Starter
    Junior Member
    Join Date
    Nov 2008
    Posts
    21

    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
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.