Results 1 to 16 of 16

Thread: Array Bounds

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2003
    Location
    Argentina
    Posts
    1,950

    Array Bounds

    This should be simple, but it's not working.
    The code below should show (to my mind) the values in the first dimension of the array, i.e., 12, 12, 13,...20.
    Instead it shows 12, 12, then stops. This must be something obvious, but I can't see what the problem is. Any ideas? Thanks.

    VB Code:
    1. Dim i, j, l As Integer
    2. Static x As Integer
    3. Dim arrQuNumbers(,) = {{"12", "12", "13", "14", "15", "15", "15", "16", "17", "18", "20"}, _
    4.                      {"16.1", "16.2", "17", "18", "19.1", "19.2", "19.3", "20", "21", "22", "24"}}
    5.  
    6.      
    7. For i = arrQuNumbers.GetLowerBound(0) To arrQuNumbers.GetUpperBound(0)
    8.       l = arrQuNumbers(0, i)
    9.       MessageBox.Show(l)
    10. Next

  2. #2
    Frenzied Member
    Join Date
    Mar 2004
    Location
    Orlando, FL
    Posts
    1,618
    Maybe change it to:

    l = arrQuNumbers(i, 0)

    Is it row column, or column row, I don't remember offhand...

    EDIT: Looking at that closer, you have an 11 columned array with two records in it? Is that what you wanted? Ugh, it's too late on Friday to have to think....
    Last edited by SeanGrebey; Jun 11th, 2004 at 03:39 PM.
    Sean

    Some days when I think about the next 30 years or so of my life I am going to spend writing code, I happily contemplate stepping off a curb in front of a fast moving bus.

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2003
    Location
    Argentina
    Posts
    1,950
    No, arrQuNumbers(i, 0) shows 12, 16 and stops.
    What this should be is a two dimensioned array in 2 x 11 size.

  4. #4
    Frenzied Member
    Join Date
    Mar 2004
    Location
    Orlando, FL
    Posts
    1,618
    Well, change it to

    For i = arrQuNumbers.GetLowerBound(1) To arrQuNumbers.GetUpperBound(1) and it works. Maybe it is 0 is number of columns and 1 is number of rows? I can't say I ever do much with multiple dimension arrays, so it's all just speculation, but I tried it and it works that way
    Sean

    Some days when I think about the next 30 years or so of my life I am going to spend writing code, I happily contemplate stepping off a curb in front of a fast moving bus.

  5. #5
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949
    Originally posted by SeanGrebey
    Well, change it to

    For i = arrQuNumbers.GetLowerBound(1) To arrQuNumbers.GetUpperBound(1) and it works. Maybe it is 0 is number of columns and 1 is number of rows? I can't say I ever do much with multiple dimension arrays, so it's all just speculation, but I tried it and it works that way
    It is misleading to look upon two dimensional arrays as Rows & Columns as in a table. Values are only stored in the elements of the final dimension of a multi-dimension array. No matter how many dimensions you have, all dimensions are represented by numbers which together identify the unique location at which a value can be stored. Whereas in a table you can have data stored in any row of any column

    Also, you must be working with Option Strict Off to code like you have, which is not recommended.
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

  6. #6

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2003
    Location
    Argentina
    Posts
    1,950
    Sorry taxes, but that doesn't help. I could do it with two different arrays; that's how I did it with the Access VBA program I'm trying to replace.
    I'd try to do more now, but too much of Reagan stuff is going on.

  7. #7
    Frenzied Member <ABX's Avatar
    Join Date
    Jul 2002
    Location
    Canada eh...
    Posts
    1,622
    VB Code:
    1. 'First Of All Lets Format the Array so that it is easy to see the mistake
    2.         Dim arrQuNumbers(,) = { _
    3.                             {"12", "12", "13", "14", "15", "15", "15", "16", "17", "18", "20"}, _ ' Row 1 (index 0)
    4.                             {"16.1", "16.2", "17", "18", "19.1", "19.2", "19.3", "20", "21", "22", "24"} _ ' Row 2 (index 1)
    5.                              }
    6.  
    7.         'Now I am going to explain this a little deeper w/ code
    8.  
    9.         MsgBox("The First Array Bound (0) has a Upperbound of: " & arrQuNumbers.GetUpperBound(0))
    10.         MsgBox("The Second Array Bound (1) has a Upperbound of: " & arrQuNumbers.GetUpperBound(1))
    11.  
    12.         'Here is a simple table of your values and how it corrisponds to the array bounds
    13.  
    14.         'Assume the first bound is the column index(x) and the second bound is the row index(y)
    15.  
    16.         'Table
    17.         '  |   0     1     2   3   4     5       6   7   8   9  10
    18.         '---------------------------------------------------------
    19.         '0 |    12,   12, 13, 14,   15,   15,   15, 16, 17, 18, 20
    20.         '1 |  16.1, 16.2, 17, 18, 19.1, 19.2, 19.3, 20, 21, 22, 24
    21.  
    22.         'So Now that you understand that (1,1) Refers to  value 16.2 Then
    23.  
    24.         MsgBox("This Value: arrQuNumbers(1, 1) is also " & arrQuNumbers(1, 1))
    25.  
    26.         'So to do what you orginally wanted to do ....
    27.  
    28.         For I As Integer = 0 To arrQuNumbers.GetUpperBound(1)
    29.             MsgBox(arrQuNumbers(0, I))
    30.         Next
    31.  
    32.         'Now to display both rows....
    33.  
    34.         For x As Integer = 0 To arrQuNumbers.GetUpperBound(0)
    35.             For j As Integer = 0 To arrQuNumbers.GetUpperBound(1)
    36.                 MsgBox(arrQuNumbers(x, j))
    37.             Next
    38.         Next

    Edit: Missed a 15 in first row ;(
    Last edited by <ABX; Jun 12th, 2004 at 01:55 PM.
    Tips:
    • Google is your friend! Search before posting!
    • Name your thread appropriately... "I Need Help" doesn't cut it!
    • Always post your code!!!! We can't read your mind!!! (well, at least most of us!)
    • Allways Include the Name and Line of the Exception (if one is occuring!)
    • If it is relevant state the version of Visual Studio/.Net Framwork you are using (2002/2003/2005)


    If you think I was helpful, rate my post
    IRC Contact: Rizon/xous ChakraNET/xous Freenode/xous

  8. #8
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    How come you don't have Option Explicit on?

    Here, try this code:

    VB Code:
    1. Dim i, j As Integer
    2.         Dim l As Double
    3.  
    4.         Static x As Integer
    5.         Dim arrQuNumbers(,) As Decimal = {{12, 12, 13, 14, 15, 15, 15, 16, 17, 18, 20}, _
    6.                              {CDec("16.1"), CDec("16.2"), 17, 18, CDec("19.1"), CDec("19.2"), CDec("19.3"), 20, 21, 22, 24}}
    7.  
    8.         For i = 0 To UBound(arrQuNumbers, 2)
    9.             l = arrQuNumbers(0, i)
    10.             MessageBox.Show(l.ToString)
    11.  
    12.         Next

    The only difficulty I faced was placing the decimal values into the array. I think I have done it correctly.

  9. #9
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    I wasn't aware of ABX's post, dangnabbit.

    Oh, and leave the dead white guy alone. Write your programs.

  10. #10
    Frenzied Member <ABX's Avatar
    Join Date
    Jul 2002
    Location
    Canada eh...
    Posts
    1,622
    yeah.... i was surpirised that someone else didnt catch the mistake....

    Those kind of mistakes cost me hours and hours of screwing around ;(
    Tips:
    • Google is your friend! Search before posting!
    • Name your thread appropriately... "I Need Help" doesn't cut it!
    • Always post your code!!!! We can't read your mind!!! (well, at least most of us!)
    • Allways Include the Name and Line of the Exception (if one is occuring!)
    • If it is relevant state the version of Visual Studio/.Net Framwork you are using (2002/2003/2005)


    If you think I was helpful, rate my post
    IRC Contact: Rizon/xous ChakraNET/xous Freenode/xous

  11. #11
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    Hey, at least it isn't a case sensitive language.


  12. #12
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949
    Originally posted by salvelinus
    Sorry taxes, but that doesn't help. I could do it with two different arrays; that's how I did it with the Access VBA program I'm trying to replace.
    I'd try to do more now, but too much of Reagan stuff is going on.
    I obviously have not made myself clear.

    In your first post you said

    "The code below should show (to my mind) the values in the first dimension of the array, i.e., 12, 12, 13"

    That is the point to which I was referring. All values were stored in the Second Dimension. Your second post clearly indicates you knew the array was of 2 dimensions with 2 elements in the first dimension and 11 elements in the second dimension.
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

  13. #13

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2003
    Location
    Argentina
    Posts
    1,950
    Thanks, guys, I'll try those out at work Monday.
    The Option question isn't up to me, but I'll bring it up to those whom it is.
    I've tried different ways. I'll try these. May be getting confused between the different vb versions I work with. Rarely use multidimension arrays. What this should eventually do is loop through some text, adding the value in the second dimension to the text after the occurence of the first dimension in the text (not replace it).
    I did this in Access VBA with two separate arrays, and could do the same here. But I'm trying to write optimized code. Sometimes bugs the boss, who wants it Now, but I want it the best I can do.
    I'm not sure where the mistake that <abx> says occurs. I know that arrQuNumbers(1, 1) refers to 16.2. So arrQuNumbers(0, i) should loop through the elements in the first dimension. But that didn't work either.
    Needless to say, this is just test code to see how it works.

  14. #14

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2003
    Location
    Argentina
    Posts
    1,950
    Originally posted by taxes
    I obviously have not made myself clear.

    In your first post you said

    "The code below should show (to my mind) the values in the first dimension of the array, i.e., 12, 12, 13"

    That is the point to which I was referring. All values were stored in the Second Dimension. Your second post clearly indicates you knew the array was of 2 dimensions with 2 elements in the first dimension and 11 elements in the second dimension.
    Uh, no, I thought there were 11 elements in both dimensions. I thought the array was 2 rows, 11 columns each. Used an example from a book. Where was my mistake in the first dimension? Thanks, I really appreciate the help. This has got to be a brain fart issue.

  15. #15
    Frenzied Member <ABX's Avatar
    Join Date
    Jul 2002
    Location
    Canada eh...
    Posts
    1,622
    your mistake was:

    VB Code:
    1. For i = arrQuNumbers.GetLowerBound(0) To [b]arrQuNumbers.GetUpperBound(0)[/b]
    2.       l = arrQuNumbers(0, i)
    3.       MessageBox.Show(l)
    4. Next

    This should be GetUpperbound(1) because

    GetUpperbound(0) will return the value 1 because it is refering to the row index

    thats why you get 12,12 because the first two numbers are both 12's

    arrQuNumbers(0,0) = 12
    arrQuNumbers(0,1) = 12
    the second index is the value that refers to the column
    now the other value refers to the row index so you are saying that you want to access values (0,0), (0,1)

    btw: GetLowerBound isn't required any more because arrays must have a base of 0
    Tips:
    • Google is your friend! Search before posting!
    • Name your thread appropriately... "I Need Help" doesn't cut it!
    • Always post your code!!!! We can't read your mind!!! (well, at least most of us!)
    • Allways Include the Name and Line of the Exception (if one is occuring!)
    • If it is relevant state the version of Visual Studio/.Net Framwork you are using (2002/2003/2005)


    If you think I was helpful, rate my post
    IRC Contact: Rizon/xous ChakraNET/xous Freenode/xous

  16. #16
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949
    Originally posted by salvelinus
    What this should eventually do is loop through some text, adding the value in the second dimension to the text after the occurence of the first dimension in the text (not replace it).
    I did this in Access VBA with two separate arrays, and could do the same here. But I'm trying to write optimized code. Sometimes bugs the boss, who wants it Now, but I want it the best I can do.
    I'm not sure where the mistake that <abx> says occurs. I know that arrQuNumbers(1, 1) refers to 16.2. So arrQuNumbers(0, i) should loop through the elements in the first dimension. But that didn't work either.
    Needless to say, this is just test code to see how it works.
    Referring to the above and ABX's last post, you can ONLY use the reference to Rows & Columns in Two dimensional arrays. But, even then, THEY DO NOT FUNCTION LIKE ROWS & COLUMNS. There are no values at all stored in the Row number. The values are stored only in the elements held in the last dimension. The values of the first dimension are solely the Row numbers, normally starting at 0 ( but in VB.NET you can create dimensions based on other numbers - with great difficulty and it is not CLS compliant so you will have difficulty if you ever want to share them with other .Net languages and don't even think about it for a single dimension array (also called Vectors)).

    If you want to store two separate values relating to a common unifier then you will HAVE to use two separate 2 dimension arrays, using the first (and second if necessary) dimension numbers to link those values. In it's simplest form arrTest1(3,0) carries values which you want to which you want to relate in arrTest2(3,0). Most people never have any need to go beyond a two dimensional array. I suppose you could design a two dimension array where the first dimension denotes the class of data involved and then expand the second dimension to a point where if could hold sufficient data to achieve your objective, but there is insufficient information on your objective to determine this.

    With regard to ABX's last post, in GetUpperBound(0) the 0 gets the upperbound of the FIRST dimension and that is NOT where the values are stored. GetUpperBound(1) gets the upperbound of the SECOND dimension, where the values ARE stored. You do, of course, need to know the upperbounds of all the dimensions(in your case both dimensions) in order to extract all the values
    Boy, I'm glad you are not working in 5 dimension arrays
    Last edited by taxes; Jun 12th, 2004 at 06:52 PM.
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

Posting Permissions

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



Click Here to Expand Forum to Full Width