locked
Basic array quesiton RRS feed

  • Question

  • User-1417960827 posted

    What is the difference between using the  'array' class in VB and just declaring an array like normal.   In other words, what is the difference between:

     

    Dim numOfTicketsPerDay As Array

    Dim intNumOfTicketsPerDay(6) As Integer

     

    Is there any preference of when one should be used over another?

     

    thx

    Friday, March 12, 2010 11:27 AM

Answers

  • User-1360095595 posted

    I would say an important difference is that the Array class has many methods that you can use like Sort(), Reverse(), Find(), Clear(), etc...  If you were declaring the array yourself, you'd need to write the code to do all this; that is if you need to use any of them.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, March 12, 2010 12:48 PM
  • User-952121411 posted

    The (2) biggest difference also between those (2) lines of code is as follows:

    1. With the 1st declaration of 'Array' you have not given it a dimension (size) as you have with the Integer declaration.  Therefore, you are going to need to 'ReDim' or re-dimension the array to add space to add any values to it.  This does not happen dynamically as it can in an object like an ArrayList.
    2. With the declaration of 'Array' you are allowing any type to be added to each level of the array.  For example, if the size was '2', then you could have numOfTicketsPerDay(0) = 50 & numOfTicketsPerDay = "Fifty".  With the array of Integer values declared in the second line, only integers can be placed in it.  The second line is more explicit, but as mentioned before does not expose all of the methods available like an Array object type does.

    My advice.  Use neither.  Instead use an ArrayList.  They are 10x times easier to work with in this scenario IMO.

            Dim numOfTicketsPerDay As New ArrayList
    
            numOfTicketsPerDay.Add(50) 'Location '0'
            numOfTicketsPerDay.Add(75) 'Location '1'
            numOfTicketsPerDay.Add(80) 'Location '2'


     

    Hope this helps! Smile

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, March 15, 2010 11:15 AM

All replies

  • User-1360095595 posted

    I would say an important difference is that the Array class has many methods that you can use like Sort(), Reverse(), Find(), Clear(), etc...  If you were declaring the array yourself, you'd need to write the code to do all this; that is if you need to use any of them.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, March 12, 2010 12:48 PM
  • User-952121411 posted

    The (2) biggest difference also between those (2) lines of code is as follows:

    1. With the 1st declaration of 'Array' you have not given it a dimension (size) as you have with the Integer declaration.  Therefore, you are going to need to 'ReDim' or re-dimension the array to add space to add any values to it.  This does not happen dynamically as it can in an object like an ArrayList.
    2. With the declaration of 'Array' you are allowing any type to be added to each level of the array.  For example, if the size was '2', then you could have numOfTicketsPerDay(0) = 50 & numOfTicketsPerDay = "Fifty".  With the array of Integer values declared in the second line, only integers can be placed in it.  The second line is more explicit, but as mentioned before does not expose all of the methods available like an Array object type does.

    My advice.  Use neither.  Instead use an ArrayList.  They are 10x times easier to work with in this scenario IMO.

            Dim numOfTicketsPerDay As New ArrayList
    
            numOfTicketsPerDay.Add(50) 'Location '0'
            numOfTicketsPerDay.Add(75) 'Location '1'
            numOfTicketsPerDay.Add(80) 'Location '2'


     

    Hope this helps! Smile

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, March 15, 2010 11:15 AM