Computer Science Canada

2D Array Glitch, this never happened before...

Author:  ZeroPaladn [ Tue Jan 23, 2007 9:25 am ]
Post subject:  2D Array Glitch, this never happened before...

VisualBASIC:
Option Explicit
Dim libraryArray() As String ' Stores Library Contents
Dim i As Integer, j As Integer, m As Integer, l As Integer ' Reserved for FOR LOOPS
Dim helpTopic As String ' Carries the help topic string used for the mini help
Dim addFlag As Boolean, exitFlag As Boolean, saveFlag As Boolean, deleteFlag As Boolean ' Flags used for adding an entry, deleting an entry, and exiting the program

Private Sub displayRefresh(arrayNumber As Integer)
    Dim p As Integer
    txtDisplay.Text = ""
    For p = 0 To arrayNumber Step 1
        If p = 0 Then
        Else
            txtDisplay.Text = txtDisplay.Text + libraryArray(p, 0) + ", " + libraryArray(p, 1) + ", " + libraryArray(p, 2) + vbNewLine
        End If
    Next p
End Sub

Private Sub search(searchType As Integer)
    Dim h As Integer
    txtDisplay.Text = ""
    For h = 0 To i Step 1
        If h = 0 Then
        Else
            If txtSearch.Text = libraryArray(h, searchType) Then
                txtDisplay.Text = txtDisplay.Text + libraryArray(h, 0) + ", " + libraryArray(h, 1) + ", " + libraryArray(h, 2) + vbNewLine
            End If
        End If
    Next h
End Sub

Private Sub btnAdd_Click()
    addFlag = True
    For j = 0 To i Step 1
        If j = 0 Then
            If txtAddAlbum.Text = libraryArray(j, 0) And txtAddArtist.Text = libraryArray(j, 1) Then
                MsgBox ("Entry allready exists.")
                addFlag = False
            ElseIf txtAddNumber.Text = libraryArray(j, 2) Then
                MsgBox ("ID# in use.")
                addFlag = False
            End If
        End If
    Next j
    If addFlag = True Then
        i = i + 1
        ReDim libraryArray(0 To i, 2)
        libraryArray(i, 0) = txtAddAlbum.Text
        libraryArray(i, 1) = txtAddArtist.Text
        libraryArray(i, 2) = txtAddNumber.Text
    End If
    displayRefresh (i)
End Sub

Private Sub btnAlbumSearch_Click()
    search (0)
End Sub

Private Sub btnArtistSearch_Click()
    search (1)
End Sub

Private Sub btnDelete_Click() ' Deletes an entry inside the file and rewrites the file

End Sub

Private Sub btnDisplayReset_Click() ' Resets the display so it will show all enteries inside the file
    displayRefresh (i)
End Sub

Private Sub btnExit_Click() ' Used to exit the program
    Open "H:\ICS 3MI\Visual Basic\Final Project\libraryFile.txt" For Output As 1
    For j = 1 To i Step 1
        Write #1, libraryArray(j, 0), libraryArray(j, 1), libraryArray(j, 2)
    Next j
    Close #1
    End
End Sub

Private Sub btnHelp_Click() ' Makes the mini help file pop up
    helpTopic = InputBox("Enter the keyword for the help you require. 'sort' will give you help for sorting your data. 'add' will give you help for adding new data. 'search' will give you help with the search function. 'delete' will tell you how to delete an entry. 'display' will tell you about the Display All button.")
    If helpTopic = "sort" Then
        MsgBox ("Clicking on Alpha - Album will sort the records in alphabetical order by their album names, Alpha - Author will sort it in alphabetical order by the names of the band, while ID # will sort by the ID numbers, from lowest to highest.")
    ElseIf helpTopic = "add" Then
        MsgBox ("To add new data, fill in the required headings above the button (Album, Author, and ID #) and click on the Add button. If one or more of the fields are incorrect, you wil have to follow the prompt to correct it. The program will not add an album that is allready in the library.")
    ElseIf helpTopic = "search" Then
        MsgBox ("Type in the full or partial name of the album or artist that you are looking for, or the ID number. add a * before your search word to enable a search for partial matches (ex. *Alex).")
    ElseIf helpTopic = "delete" Then
        MsgBox ("Type in the ID# of the item you wish to delete, then hit the delete button, a promp will ask you if you really want to do this. NOTE: Not useable at the moment.")
    ElseIf helpTopic = "display" Then
        MsgBox ("This button will display all enteries inside the file. Use this button after a search to re-display all the available enteries.")
    Else
        MsgBox ("Invalid keyword.")
    End If
End Sub

Private Sub btnNumberSearch_Click() ' Searches for en entry using the given number entered
    search (2)
End Sub

Private Sub Form_Load() ' Loads file to be used by the program and initializes display
    Open "H:\ICS 3MI\Visual Basic\Final Project\libraryFile.txt" For Input As 1
    Do Until EOF(1) ' Exits file when it reaches the end of it
        i = i + 1 ' Adds one to itself, used with the ReDim function so it can accept more enteries
        ReDim libraryArray(i, 2) ' Reset libraryArray so it can accept more enteries
        Input #1, libraryArray(i, 0), libraryArray(i, 1), libraryArray(i, 2)
    Loop
    Close #1
    displayRefresh (i)
    txtAddNumber.MaxLength = 3
    txtDelete.MaxLength = 3
End Sub


Although i don't get an arror when I run this (for the most part), During the Form_Load, I load everything from the file and stores it into the array, and ReDim the array to fit more untill the end of the file, heres the catch, when it loads a file that looks liek this...
code:
"Blink 182","Blink 182","000"
"The Sufferer and the Witness","Rise Against","003"

It only stores the last element read, and anythign else before it is an empty string. The display on the screen and when re-saved to the file, it shows this...
code:
"","",""
"The Sufferer and the Witness","Rise Against","003"

This sucks. I've troubleshooted it to prove that its the reading of the file and saving to the array thats the trouble. Can anybody help? I can't provide the form to run the code (unallowed to upload :\) so this is all compsci gets.

Thanks in advance guys!

Author:  Silent Avenger [ Tue Jan 23, 2007 11:25 am ]
Post subject:  Re: 2D Array Glitch, this never happened before...

There's an easy way to get around this. Since you're saving in a sequential file I suggest saving and loading the data from the file in a sequential manner. If you save your three peices of data one after the other you'll always have it in that format.
eg
open filepath for output as #1
print #1, data1
print #1, data2
print #1, data3
close #1

if you do this in a loop you're file will look somewhat like this:

data1
data2
data3
data1
data2
data3

so if you load the file in the same way all your data will be in the right position

open filepath for input as #1
line input #1, data1
line input #1, data2
line input #1, data3
close #1


If you need anymore explination on what I'm talking about just ask.

Author:  ZeroPaladn [ Tue Jan 23, 2007 12:52 pm ]
Post subject:  Re: 2D Array Glitch, this never happened before...

I've implemented your idea of using Print intead of Write and there is no change to the data being saved to the array.

Good News - The source of the problem is now narrowd down to where the data is being saved, but that cant possible mess up, so it must be the ReDiming the array that is causing the problem. I'm wondering if declaring ReDim libraryArray(i, 2) causes the array to empty...

Bad News - If this is the case, how can I get around that wihout having a static array, or forcing myself to declare 3 sepearate 1D arrays, provided that you can ReDim any type of array without it emptying?

Author:  ZeroPaladn [ Tue Jan 23, 2007 1:11 pm ]
Post subject:  Re: 2D Array Glitch, this never happened before...

Ha! I knew it! when calling ReDim, it clears the array, and since i got Option Explicit on, it automatically adds an empty string to the array.

The Solution:
VisualBASIC:
ReDim Preserve libraryArray(i, 2)

Preserve does what it says, its preserves the array so the data isn't deleted. One problem with it though, only the last element of the array can be modified once the array has be Dimed, so i had to reverse it items inside the array. Not a big change, but it took alot of editing to the program.

Thanks for your help Silent Avenger, even though I figured it out on my own, the Print method of saving and reading data saved me alot of problems since using the Write method scrambled my data a little.

Author:  Silent Avenger [ Tue Jan 23, 2007 2:19 pm ]
Post subject:  Re: 2D Array Glitch, this never happened before...

Umm I'm not sure if preserve works for 2D arrays because I remember we spent a whole day of class trying to find out why someone was getting the subscript out of range error. We found out that we could redim a 2d array but not preserve it. If you do get this error tell me because there is another way.

Author:  ZeroPaladn [ Tue Jan 23, 2007 2:33 pm ]
Post subject:  Re: 2D Array Glitch, this never happened before...

The entire thing works fine. The thing that you were talking about on how your array was getting out of range was because you can only modify the last element of the array when you are declaring Preserve. I read it in the help file (i should do that more often, it would stop these stupid posts). so if you attemped to modify another chunk of that array, then tried to call a parameter of that array that is lower or higher than your first specified array, youll get the error.

Author:  Silent Avenger [ Tue Jan 23, 2007 4:45 pm ]
Post subject:  Re: 2D Array Glitch, this never happened before...

Oh I see, yeah we were trying to figure that out and VB wasn't installed properly on our systems so we didn't have the help files. Haha sucks to be my friend then, he spent 30 min trying to make a function that will re input the data into the array. Anyways you could have also used a UDT instead of a 2D array.

Author:  ZeroPaladn [ Wed Jan 24, 2007 8:13 am ]
Post subject:  Re: 2D Array Glitch, this never happened before...

could you elaborate for me what a UTD is?

Author:  Silent Avenger [ Wed Jan 24, 2007 10:36 am ]
Post subject:  Re: 2D Array Glitch, this never happened before...

A UDT is short for user define type. So instead of having a 2D array you can have a variable declared as a UDT and withing the UDT you can have different fields. Here's an example:

VB:
Type car
    make As String
    model As String
    year As Integer
    colour As String
End Type

Dim carstat As car

Private Sub Command1_Click()
carstat.colour = "Red"
carstat.make = "Ferrari"
carstat.model = "Enzo"
carstat.year = 2005
End Sub


The type is supposed to be in a module unless you make it private. But using a UDT instead of a2D array makes the program a whole lot more organized.

Author:  Silent Avenger [ Wed Jan 24, 2007 10:36 am ]
Post subject:  Re: 2D Array Glitch, this never happened before...

A UDT is short for user define type. So instead of having a 2D array you can have a variable declared as a UDT and withing the UDT you can have different fields. Here's an example:

VB:
Type car
    make As String
    model As String
    year As Integer
    colour As String
End Type

Dim carstat As car

Private Sub Command1_Click()
carstat.colour = "Red"
carstat.make = "Ferrari"
carstat.model = "Enzo"
carstat.year = 2005
End Sub


The type is supposed to be in a module unless you make it private. But using a UDT instead of a2D array makes the program a whole lot more organized.

Author:  ZeroPaladn [ Wed Jan 24, 2007 10:47 am ]
Post subject:  Re: 2D Array Glitch, this never happened before...

Ah! You could have simply said it was like a record in turing...
Turing:

type car
    record
        make : string
        model : string
        year : int
        kolour : string
    end record

var carStat : car

carStat.kolour := "Red"
carStat.make := "Ferrari"
carStat.model := "Enzo"
carStat.year := 2005

And I get on how it would make my program a little more organized, but would I be able to redefine a UDT like...
VisualBASIC:
Type library
    album as string
    artist as string
    idnumber as integer
End Type

Dim libraryArray() as library

ReDim Preserve libraryArray(i) ' Something like this?

Author:  Silent Avenger [ Wed Jan 24, 2007 12:24 pm ]
Post subject:  Re: 2D Array Glitch, this never happened before...

Yeah I could have said that but I don't like turing and don't program in it so I didn't know. But yeah you can use that code for your program. I don't really think it makes it more simplified but it does make it a lot more organized and easier to follow.


: