By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,379 Members | 3,035 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,379 IT Pros & Developers. It's quick & easy.

Help With a Dynamic Array in VB6

P: 4
Hello,

I am fairly new to VB6 and I am experiencing a problem with a dynamic array. I made a program that calculates Body Mass Index and saves each individual BMI number to a sequential disk file. I have set up a Summary on another form that will break down all of the saved BMI numbers by percentage.

I am using a dynamic array to load the information from the disk file, but for some reason it is not inputing the last record in the file. For example, if there are 4 records saved to the disk, the summary is only indicating 3 records.

I'm not sure what I am missing and I have not been able to find much information on dynamic arrays in my books or this forum. The following is the section of my code including the dynamic array. Any suggestions or help would be much appreciated!

Thanks,
Keith


Expand|Select|Wrap|Line Numbers
  1. Open "summary.dat" For Input As #FileID
  2.   Do Until EOF(FileID)
  3.       ReDim Preserve sglSummary(0 To i)
  4.       Input #FileID, sglSummary(i)
  5.       i = i - Not (EOF(FileID))
  6.   Loop
  7.   Close #FileID
Apr 22 '07 #1
Share this Question
Share on Google+
6 Replies


Expert 5K+
P: 8,434
Two questions:
  1. Can we assume that i is definitely zero before this code runs?
  2. Are you sure that your array isn't four occurences, but you're thinking it's three because they are numered 0 to 3? (Trust, me, it's an easy one to miss.)

By the way... clever idea, the way you're incrementing i by inverting the True value. :)
Apr 22 '07 #2

P: 4
Two questions:
Can we assume that i is definitely zero before this code runs?
Are you sure that your array isn't four occurences, but you're thinking it's three because they are numered 0 to 3? (Trust, me, it's an easy one to miss.)
Thanks for your quick response!

1. Yes, just before this code, I have i = 0.

2. I was wondering the same thing, but in the summary it has 3 as the total number of BMI records. I double-checked the "summary.dat" file and it physically has 4 records stored in it. I put in a break point right after this code and it shows i = 3, which actually should be correct since my file has 4 records. My array should be sglSummary(0 to 3), correct?

Perhaps the code I have next is what is incorrect. I am using Select Case to count how many BMI records are less than 20, between 20 and 22, etc:

Expand|Select|Wrap|Line Numbers
  1. For n = 1 To i
  2.         Select Case sglSummary(n)
  3.             Case Is < 20#
  4.                 intLess20 = intLess20 + 1
  5.             Case 20# To 22.59
  6.                 int20to22 = int20to22 + 1
  7.             Case 22.6 To 25.59
  8.                 int23to25 = int23to25 + 1
  9.             Case 25.6 To 30.59
  10.                 int26to30 = int26to30 + 1
  11.             Case Is > 30.6
  12.                 intGreater30 = intGreater30 + 1
  13.         End Select
  14.         intTotalCount = intTotalCount + 1
  15.     Next
  16.  
  17.     intLess20Percentage = intLess20 / intTotalCount
  18.     int20to22Percentage = int20to22 / intTotalCount
  19.     int23to25Percentage = int23to25 / intTotalCount
  20.     int26to30Percentage = int26to30 / intTotalCount
  21.     intGreater30Percentage = intGreater30 / intTotalCount
After running the program again, I noticed that the 3 records counted in the summary all show up in the "Less Than 20" category, but in fact all 4 records in the file are larger than 20. Obviously, something is wrong here and could be my overall problem.


Thanks for your help!
Keith
Apr 22 '07 #3

Expert 5K+
P: 8,434
Actually, I think your problem is in this line...
For n = 1 To i
Didn't we just establish that the array is numbered from zero to three? This being the case, in this line you would appear to be skipping the first element.

For this and other reasons, I always start my arrays at element 1 rather than 0. The zero-based version is too confusing - humans just don't think that way.
Apr 22 '07 #4

P: 4
Actually, I think your problem is in this line...
For n = 1 To i
Yep, that's one I should have noticed! Thanks.

That corrected the issue with the number of records, but it is still showing them all in the "Less than 20" catergory, but all four records in the file are in fact more than 20. Is there something coded wrong in the Select Case? Maybe the Select Case is not reading what is stored in the Array for some reason?? I'm stumped.

Thanks for your help!
Keith
Apr 22 '07 #5

Expert 5K+
P: 8,434
Well, I'd suggest you print out the values in the loop. At a guess, they may all be zero. You might need to input into a string then convert, rather than input directly from the file into the numeric.
Apr 23 '07 #6

P: 4
I appreciate all of your help on this. Everything works now.


Thanks,
Keith
Apr 24 '07 #7

Post your reply

Sign in to post your reply or Sign up for a free account.