Wrox Programmer Forums
|
Beginning VB 6 For coders who are new to Visual Basic, working in VB version 6 (not .NET).
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Beginning VB 6 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 20th, 2004, 04:32 PM
OC OC is offline
Registered User
 
Join Date: Mar 2004
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default Dynamic array - please help

Here's part of my code:

Public strA As String
Public numA As Integer
Public numB As Integer
Public numC As Integer
Public X As Integer
Public Y As Integer
Dim Misli() As String

Open App.Path & "\MD3.dat" For Random As #1

numA = 0
numC = 0
numB = 0

ReDim Preserve Misli(1 To 1, 1 To 10, 1 To 20)

Get #1, 1, strA

For X = 1 To 10
    For Y = 1 To 20
        Misli(numA + 1, X, Y) = "0"
    Next Y
Next X

Do Until EOF(1)
    Get #1, , strA
    If strA = "@" Then
        ReDim Preserve Misli(1 To numA + 1, 1 To 10, 1 To 20)
        For X = 1 To 10
            For Y = 1 To 20
                Misli(numA + 1, X, Y) = "0"
            Next Y
        Next X
        numA = numA + 1
        numB = 1
        numC = 0
    Else
        If strA = "¤" Then
            numB = numB + 1
            numC = 0
        Else
            Misli(numA + 1, numB + 1, numC + 1) = strA 'the error line
             numC = numC + 1
        End If
    End If
    DoEvents
Loop

Close #1

The first time the commands under the loop are performed everything's ok, at the second time I get a subscript out of range error in the marked error line (Misli(numA + 1, numB + 1, numC + 1) = strA) above. Why?

I need the three-dymensional array because the data is stored like this: Misel(number of the current text, number of the current line, number of the current word) = current word

The read "@" means a new text starts and the "¤" means a new line begins. i need the texts stored this way.

The number of words change in every line so I don't have a fixed number of them in every line but there are always less than 20 words in them and the number of lines is always 10. I thought I need to fill all the empty items in the array to ReDim Preserve it (not sure if I do but no harm is done), so I fill all spaces with "0" and then overwrite relavant ones with data. Any other ideas how to solve this prob?

I would be very grateful for a solution.
 
Old March 20th, 2004, 09:33 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,998
Thanks: 0
Thanked 3 Times in 3 Posts
Default

YOu have this in the IF routine.

ReDim Preserve Misli(1 To numA + 1, 1 To 10, 1 To 20)
..
numA = numA + 1

At the time the first statement executes, numA = 0, which would be 1 to 1 again, which at this point, you want it to be 1 to 2, because of the numA increment in the second statement. Then when the else statement hits the next round, it's reference 2, when the maximum is one.

Try that,

Brian
 
Old March 21st, 2004, 04:08 AM
OC OC is offline
Registered User
 
Join Date: Mar 2004
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanx Brian!

You helped me get a step closer, but now I have a new prob. The same code (adjusted) only this time the problem appears in the ReDim Preserve line when the second text begins and strA="@" for the first time and numA=0

Public strA As String
Public numA As Integer
Public numB As Integer
Public numC As Integer
Public X As Integer
Public Y As Integer
Dim Misli() As String

Open App.Path & "\MD3.dat" For Random As #1

numA = 0
numC = 0
numB = 0

ReDim Misli(1 To 1, 1 To 10, 1 To 20)

Get #1, 1, strA

For X = 1 To 10
    For Y = 1 To 20
        Misli(numA + 1, X, Y) = "0"
    Next Y
Next X

Do Until EOF(1)
    Get #1, , strA
    If strA = "@" Then
        ReDim Preserve Misli(1 To numA + 2, 1 To 10, 1 To 20) ' here's the error
        For X = 1 To 10
            For Y = 1 To 20
                Misli(numA + 2, X, Y) = "0"
            Next Y
        Next X
        numA = numA + 1
        numB = 1
        numC = 0
    Else
        If strA = "¤" Then
            numB = numB + 1
            numC = 0
        Else
            Misli(numA + 1, numB + 1, numC + 1) = strA
            numC = numC + 1
        End If
    End If
    DoEvents
Loop

Close #1

I still don't get it. The previous array was redeclared as Misli(1 to 1, 1 to 10, 1 to 20), why can't I redeclare and preserve it later in Misli(1 to 2, 1 to 10, 1 to 20)?

This ReDim Preserve is giving me a headache...
 
Old March 21st, 2004, 04:19 AM
OC OC is offline
Registered User
 
Join Date: Mar 2004
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Just seen and fixed another prob. The numB must me zero at the beginning of a new text and not 1. I still have the above prob. :(

Do Until EOF(1)
    Get #1, , strA
    If strA = "@" Then
        ReDim Preserve Misli(1 To numA + 2, 1 To 10, 1 To 20) ' here's the error
        For X = 1 To 10
            For Y = 1 To 20
                Misli(numA + 2, X, Y) = "0"
            Next Y
        Next X
        numA = numA + 1
        numB = 0
        numC = 0
    Else
        If strA = "¤" Then
            numB = numB + 1
            numC = 0
        Else
            Misli(numA + 1, numB + 1, numC + 1) = strA
            numC = numC + 1
        End If
    End If
    DoEvents
Loop

Please help

 
Old March 21st, 2004, 04:59 AM
OC OC is offline
Registered User
 
Join Date: Mar 2004
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I've found some info. On another site someone stated that when you work with multi-dimensional arrays, you can only resize (while preserving) the last dimension while the others must remain the same.

That would explain the error I have.

I'm taking a new approach to the prob.

I'll let the prog count the number of "@" (texts) in the file then ReDim the array to the appropriate size. Then close the file and start all over with reading the file without resizing the array.

Hope this works.

 
Old March 21st, 2004, 05:11 AM
OC OC is offline
Registered User
 
Join Date: Mar 2004
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Jup, that solved it :D

Now I can peacefully go on with my life

Thanx again for helping me Brian!

Cao :)






Similar Threads
Thread Thread Starter Forum Replies Last Post
Dynamic picturebox array thomaz C# 3 September 17th, 2008 01:15 PM
Dynamic declaration of an array tenkasian C# 2 September 5th, 2007 10:14 PM
Dynamic array of struct bobwrx C# 2 September 14th, 2006 06:12 AM
dynamic multidemsion array harpua Classic ASP Basics 1 July 27th, 2004 02:44 PM
Dynamic Control array [email protected] Visual C++ 6 September 3rd, 2003 08:48 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.