questoin about a fxn, I guess...

1. Senior Member
Join Date
Dec 1969
Posts
413

## questoin about a fxn, I guess...

I had a problem earlier (http://www.aspmessageboard.com/forum/asp.asp?M=735078&T=735013&F=20&P=1) and MedievalDude helped me find an answer (Bill Wilkenson did, too), but why is it that this For...Next loop works,<BR><BR>For i = 1 To FNameCount<BR> FNameArray(i - 1) = Mid(FName, i, 1) <BR>Next<BR><BR>but when I changed i=0 and FNameArray(i) in the loop, I get an error, this is what I get an error on...<BR><BR>For i = 0 To FNameCount<BR> FNameArray(i) = Mid(FName, i, 1) <BR>Next<BR><BR>Just wondering...does it have to do with ReDim(Len(FName)-1)??<BR><BR>Thanks for any answers!

2. Senior Member
Join Date
Dec 1969
Posts
96,118

## Arrays start at zero, strings at one

Mid( FName, 0, 1 ) is illegal. There is NO character number 0 in a string.<BR><BR>That&#039;s why Dude had you doing<BR> FNameArray(i - 1) = Mid(FName, i, 1) <BR><BR>So that when you get character 1 from the string with MID, you put it into slot 0 of the array. Character 2 goes into slot 1, etc., etc.<BR><BR>You should NOT have changed the FOR loop code. Just the REDIM.<BR><BR>&#060;%<BR>FNameCount = Len( FName )<BR>ReDim FNameArray( FNameCount - 1 )<BR>For i = 1 To FNameCount<BR> FNameArray(i - 1) = Mid(FName, i, 1) <BR>Next<BR>%&#062;<BR><BR>*OR* you could do:<BR><BR>&#060;%<BR>FNameCount = Len( FName )<BR>ReDim FNameArray( FNameCount - 1 )<BR>For i = 0 To FNameCount-1<BR> FNameArray(i) = Mid(FName, i+1, 1) <BR>Next<BR>%&#062;<BR><BR>But the point is that the array element number needs to be one less than the character number.<BR><BR>THIS IS NOT NECESSARILY TRUE.<BR><BR>You don&#039;t SAY what you will DO with that array. If you will only use that array in your own code--never pass it off to some function that expects a standard array that starts at element zero--there is NO REASON that you have to actually USE element zero of the array!<BR><BR>So you *COULD* write the code thus:<BR><BR>&#060;%<BR>FNameCount = Len( FName )<BR>ReDim FNameArray( FNameCount )<BR>For i = 1 To FNameCount<BR> FNameArray(i) = Mid(FName, i, 1) <BR>Next<BR>%&#062;<BR><BR>Now you never put anything into element zero of the array. So just make sure you never try to use element zero and you&#039;ll be fine. And now the numbering of the array elements *matches* the numbering of the characters.<BR><BR>It&#039;s all up to you.<BR><BR>

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•