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... | ||||||
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...
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...
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:
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:
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:
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...
And I get on how it would make my program a little more organized, but would I be able to redefine a UDT like...
|
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. |