Results 1 to 18 of 18

Thread: [RESOLVED] VB6.0: I'm confused, need syntax help

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Oct 2006
    Posts
    172

    Resolved [RESOLVED] VB6.0: I'm confused, need syntax help

    Code:
    Dim MyReturn() As String
    ReDim Preserve MyReturn(0, 1) As String
    
    ReDim Preserve MyReturn((UBound(MyReturn(), 1) + 1), 1) As String
    This is giveing me a 'subscript out of range' error on the red line. What Is the correct way of doing it?

  2. #2
    Cumbrian Milk's Avatar
    Join Date
    Jan 2007
    Location
    0xDEADBEEF
    Posts
    2,448

    Re: VB6.0: I'm confused, need syntax help

    Because of the way Arrays are held in memory you can only change the last dimension (changing other dimensions involves a load of shuffling)

  3. #3
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: VB6.0: I'm confused, need syntax help

    By any chance are you trying this?

    ReDim Preserve MyReturn((UBound(MyReturn, 1) + 1), 1) As String
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  4. #4
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: VB6.0: I'm confused, need syntax help

    No Error here

    ReDim Preserve MyReturn(0 To UBound(MyReturn()), 1)

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Oct 2006
    Posts
    172

    Re: VB6.0: I'm confused, need syntax help

    Quote Originally Posted by Milk
    Because of the way Arrays are held in memory you can only change the last dimension (changing other dimensions involves a load of shuffling)
    Please elaborate. I would think that all it would really need to do is reserve enough space for 2 more elements at the end.

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Oct 2006
    Posts
    172

    Re: VB6.0: I'm confused, need syntax help

    Quote Originally Posted by koolsid
    By any chance are you trying this?

    ReDim Preserve MyReturn((UBound(MyReturn, 1) + 1), 1) As String
    That is the same thing. I just use MyReturn() to help remind myself that I'm working with an array.

  7. #7
    Cumbrian Milk's Avatar
    Join Date
    Jan 2007
    Location
    0xDEADBEEF
    Posts
    2,448

    Re: VB6.0: I'm confused, need syntax help

    Koolsid, it still won't work as you can't change anything but the last dimension a syntax that would work would be...
    Code:
    Dim MyReturn() As String
    ReDim MyReturn(1, 0) 
    ReDim Preserve MyReturn(1, (UBound(MyReturn, 2) + 1))

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Oct 2006
    Posts
    172

    Re: VB6.0: I'm confused, need syntax help

    Quote Originally Posted by jmsrickland
    No Error here

    ReDim Preserve MyReturn(0 To UBound(MyReturn()), 1)
    Because you forgot the +1

    ReDim Preserve MyReturn(0 To (UBound(MyReturn())+1), 1)

    (edit: it will still give you an error, see next reply. PS: wow you guys are fast, thanks)
    Last edited by Tontow; Apr 14th, 2008 at 05:39 PM.

  9. #9
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: VB6.0: I'm confused, need syntax help

    Koolsid, it still won't work as you can't change anything but the last dimension a syntax that would work would be...

    Are you 100% sure?

  10. #10
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: VB6.0: I'm confused, need syntax help

    Don't know if this will help but it works for me

    ReDim MyArray(0 To k, 0 To m)

    or

    ReDim MyArray(0 To k, 1)

    or

    ReDim MyArray(k, 1)

  11. #11
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: VB6.0: I'm confused, need syntax help

    I don't know if this is what you want but this didn't throw me any error

    Dim MyReturn() As String
    ReDim Preserve MyReturn(0, 1) As String
    'MsgBox UBound(MyReturn, 1) + 1
    ReDim MyReturn((UBound(MyReturn, 1) + 1), 1) As String
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  12. #12
    Cumbrian Milk's Avatar
    Join Date
    Jan 2007
    Location
    0xDEADBEEF
    Posts
    2,448

    Re: VB6.0: I'm confused, need syntax help

    Quote Originally Posted by jmsrickland
    Don't know if this will help but it works for me

    ReDim MyArray(0 To k, 0 To m)

    or

    ReDim MyArray(0 To k, 1)

    or

    ReDim MyArray(k, 1)
    You can change any dimension if you are not preserving the existing data but each of those Redim statements use the same first dimension (0 to k), try
    Code:
    Dim X()
    Redim X (1,1)
    Redim preserve X (2,1)
    it should get a subscript out of range error

  13. #13
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: [RESOLVED] VB6.0: I'm confused, need syntax help

    It occurs to me that a 2-dimensional array is that the first parameter is the number of rows and the second parameter is the number of columns so you should always be able to change the first parameter wheather you preserve or not. I am not sure about the second parameter however or do I have it backwards..

  14. #14
    Cumbrian Milk's Avatar
    Join Date
    Jan 2007
    Location
    0xDEADBEEF
    Posts
    2,448

    Re: [RESOLVED] VB6.0: I'm confused, need syntax help

    perfectly backwards...

    Hopefully this should show how the array is arranged in memory. You should be able to see how changing the last dimension involves little more than adding/losing chunks whereas anything else involves juggling.
    Code:
    Option Explicit
    Private Declare Sub RtlMoveMemory Lib "kernel32" (Destination As Any, Source As Any, ByVal Length As Long)
    
    Private Sub Form_Load()
    Dim ArrayX() As Byte, ArrayY() As Byte, i As Long
        
        ReDim ArrayX(1, 1, 2)
        ArrayX(0, 0, 0) = 1
        ArrayX(1, 0, 0) = 2
        ArrayX(0, 1, 0) = 3
        ArrayX(1, 1, 0) = 4
        ArrayX(0, 0, 1) = 5
        ArrayX(1, 0, 1) = 6
        ArrayX(0, 1, 1) = 7
        ArrayX(1, 1, 1) = 8
        ArrayX(0, 0, 2) = 9
        ArrayX(1, 0, 2) = 10
        ArrayX(0, 1, 2) = 11
        ArrayX(1, 1, 2) = 12
        
        ReDim ArrayY(11)
        RtlMoveMemory ArrayY(0), ArrayX(0, 0, 0), 12 'copys the data sequentially from one array to the other
        For i = 0 To UBound(ArrayY)
            Debug.Print ArrayY(i);
        Next i
    End Sub
    Hope it makes some sense.

  15. #15
    Frenzied Member
    Join Date
    Jun 2006
    Posts
    1,098

    Re: [RESOLVED] VB6.0: I'm confused, need syntax help

    Quote Originally Posted by jmsrickland
    It occurs to me that a 2-dimensional array is that the first parameter is the number of rows and the second parameter is the number of columns...
    If what Milk posted doesn't make sense, just think of 2D as (X, Y). Knowing that X changes as you move left/right among columns, and Y changes as you move up/down among rows, it should be easy to remember that X = Columns and Y = Rows. Thus, 2D is (Columns, Rows) and not vice versa.

  16. #16
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: [RESOLVED] VB6.0: I'm confused, need syntax help

    Here's why I thought it was row,column order. In the below code you see that I am redimensioning Beeps where I am changing the left parameter but never the right parameter. My thinking was that I was adding a new row of beep frequencies and durations each time the sub was entered.


    So Im thinking this:
    Code:
    ReDim Beeps(0 To UBound(s1), 0 To 1)
                        |        |    |
      +--------------------------+    |
      |                 |             |
      |          +--------------------+ 
      |          |      |
    freq1, duration1 <--+
    freq2, duration2 <--+
    freq3, duration3 <--+
    etc
    etc
    freqN, durationN <--+
    but, you say this is not the case so then how would that layout appear then?
    Code:
    Private Sub cmdPlayBeeps_Click()
     Dim Beeps() As Single
     
     Dim s1() As String
     Dim s2 As String
     Dim s3() As String
     Dim s4 As String
     Dim s5 As String
    
     Dim i As Integer
     
     On Error Resume Next
     
     s1 = Split(Text1.Text, vbCrLf)
        
     ReDim Beeps(0 To UBound(s1), 0 To 1)
     
     For i = 0 To UBound(s1)
       s2 = s1(i)
       
       If s2 = "" Then Exit For
       
       s3 = Split(s2, ",")
       
       s4 = s3(0)
       s5 = s3(1)
       Beeps(i, 0) = CSng(s4): Beeps(i, 1) = CSng(s5)
     Next i
      
     For i = 0 To UBound(Beeps)
       Beep Beeps(i, 0), Beeps(i, 1)
     Next i
    End Sub

  17. #17
    Cumbrian Milk's Avatar
    Join Date
    Jan 2007
    Location
    0xDEADBEEF
    Posts
    2,448

    Re: [RESOLVED] VB6.0: I'm confused, need syntax help

    the order in memory would be...
    Code:
    ReDim Beeps(0 To N, 0 To 1)
    
    Beeps(0, 0) 
    Beeps(1, 0)
    Beeps(2, 0)
    Beeps(3, 0)
    ...
    Beeps(N, 0)
    Beeps(0, 1) 
    Beeps(1, 1)
    Beeps(2, 1)
    Beeps(3, 1)
    ...
    Beeps(N, 1)
    If your using just Redim and not Redim Preserve your making a new array not adding to the old one.
    Last edited by Milk; Apr 14th, 2008 at 07:52 PM.

  18. #18
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: [RESOLVED] VB6.0: I'm confused, need syntax help

    So the graphical layout would be this instead of how I did it in my previous post.
    Code:
    Second Parameter
    |     +-- First Parameter------------>
    |     |
    0 = f1, f2, f3, f4, f5, f6, f7.........fN 
    1 = d1, d2, d3, d4, d5, d6, d7.........dN
    What I find misleading is that, for example, a 1 dimensional array would only have the X parameter which runs horizonately but when compared to a Listbox for example which is a one dimensional array you see it as one entry on top of another entry which seems to contradict the order of how array are arrainged. It seems to me that the normal way that people would think is that an array is stacked in a vertical fasion rather than a horizontal fasion

    So in a Listbox you see this

    Item 1
    Item 2
    Item 3
    Item 4

    but in reality it appears in memory like this

    Item1 Item2 Item3 Item4.....

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