|
-
Mar 25th, 2006, 02:34 PM
#1
Thread Starter
Hyperactive Member
Question about Array (Resolved)
I am having a problem with an array. Here is the code:
VB Code:
Dim strStaticZip As String = "49093"
Dim ZipArray(5) As String
ZipArray(0) = "49015"
ZipArray(1) = "49418"
ZipArray(2) = "49001"
ZipArray(3) = "49002"
ZipArray(4) = "49006"
Dim i As Integer
For i = 0 To ZipArray.Length - 1
LookUpZipCode(strStaticZip, ZipArray(i))
ListBox1.Items.Add(Format(CalcDistance(), "F").ToString)
Next
The listbox gets these numbers, which it should:
31.93
64.85
22.61
17.92
23.29
But as soon as the function is done I get this error Parameter @Zip2 has no default value. and then it adds another 23.29 to the listbox (the distance of the last zip codes entered). I only want 5 numbers returned (the five zip codes in the array). What am I doing wrong?
Last edited by FastEddie; Mar 25th, 2006 at 09:39 PM.
-
Mar 25th, 2006, 02:38 PM
#2
Re: Question about Array
I cant say much without looking at all your code..But I think you should change your loop to this ZipArray.GetUpperBound(0)
-
Mar 25th, 2006, 02:44 PM
#3
Thread Starter
Hyperactive Member
Re: Question about Array
I changed the code as you mentioned:
VB Code:
For i = 0 To ZipArray.GetUpperBound(0)
LookUpZipCode(strStaticZip, ZipArray(i))
ListBox1.Items.Add(Format(CalcDistance(), "F").ToString)
Next
I am still getting the same thing. I know it would make more sense if you could see all the code but there is a lot of code that goes along with this. I still don't understand why there are 6 loops happening and not 5 as it should.
-
Mar 25th, 2006, 04:04 PM
#4
Re: Question about Array
The line:
VB Code:
Dim ZipArray(5) As String
is defining an array with 6 elements, numbered 0 to 5.
If you want five elements, change the line to:
VB Code:
Dim ZipArray(4) As String
All things in .NET are zero-based (I believe.)
Hope it helps!
-
Mar 25th, 2006, 06:45 PM
#5
Member
Re: Question about Array
Andy_P is right, VB.net is stupid and assumes you want an array from 0 up to the size you specify. Ie
Dim array(4) As Integer
array(0) = 1
array(1) = 2
array(2) = 3
array(3) = 4
array(4) = 5
'array(5) = 6
Gives you 5 spaces in stead of the actual 4 you wanted. If you uncomment array(5) = 6 this will you a index out of range exception.
This is so stupid, even vb 6 didnt do it like this (I think). And I know that C-Style languages give you the amount of elements you specify and again the index starts at 0 like vb.net
-
Mar 25th, 2006, 06:52 PM
#6
Re: Question about Array
When declaring fixed arrays like that, it's easier to use this type of declaration:
VB Code:
Dim ZipArray() As String = {"49015", "49418", "49001", "49002", "49006"}
Then, you don't even need to think about the upper bounds and the sizes and so forth.
Bill
-
Mar 25th, 2006, 08:18 PM
#7
Re: Question about Array
 Originally Posted by (Slith++)
VB.net is stupid and assumes you want an array from 0 up to the size you specify.
I object to comments like this. VB.NET is not stupid because the number you provide is NOT the size of the array, rather it is specifically the upper bound. Arrays are zero-based because that is the widely accepted convention. Previous versions of VB were among the few languages that did not index arrays from zero. This convention arose because the number was not originally an index but rather an offset from a memoery address. The first element of an array was offset zero places from the start of the array, hence it is element zero.
-
Mar 25th, 2006, 08:29 PM
#8
Member
Re: Question about Array
No VB 6 was like every other language and the size you declared was the size you got.
This is regardless of weather its zero based or one based.
Now you need to subtract 1 from the size you want when dimensioning an array.
Ie,
Input = ("Please specify the amount of items you would like to enter into the array")
User enters 6
dim items(Input - 1) as integer
Stupid ^ now more processing
-
Mar 25th, 2006, 08:32 PM
#9
Re: Question about Array
If you had previous code that went dim blah(6) as integer, using a 1 based array, and then .NET came, all of your calls to the last element in the array would be out of range. That's alot of recoding the world over to make it zero based. The way they transitioned is better - now each array just takes up an extra element instead of breaking existing code.
Bill
-
Mar 25th, 2006, 09:38 PM
#10
Thread Starter
Hyperactive Member
Re: Question about Array
Thanks. Both changing the original Dim Array to 4 and Dimming the array like this worked.
VB Code:
Dim ZipArray() As String = { _
"49015", _
"49418", _
"49001", _
"49002", _
"49006"}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|