Results 1 to 10 of 10

Thread: Question about Array (Resolved)

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2005
    Posts
    259

    Question about Array (Resolved)

    I am having a problem with an array. Here is the code:
    VB Code:
    1. Dim strStaticZip As String = "49093"
    2.         Dim ZipArray(5) As String
    3.         ZipArray(0) = "49015"
    4.         ZipArray(1) = "49418"
    5.         ZipArray(2) = "49001"
    6.         ZipArray(3) = "49002"
    7.         ZipArray(4) = "49006"
    8.  
    9.         Dim i As Integer
    10.  
    11.         For i = 0 To ZipArray.Length - 1
    12.             LookUpZipCode(strStaticZip, ZipArray(i))
    13.             ListBox1.Items.Add(Format(CalcDistance(), "F").ToString)
    14.         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.

  2. #2
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    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)

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2005
    Posts
    259

    Re: Question about Array

    I changed the code as you mentioned:
    VB Code:
    1. For i = 0 To ZipArray.GetUpperBound(0)
    2.             LookUpZipCode(strStaticZip, ZipArray(i))
    3.             ListBox1.Items.Add(Format(CalcDistance(), "F").ToString)
    4.         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.

  4. #4
    Fanatic Member Andy_P's Avatar
    Join Date
    May 2005
    Location
    Dunstable, England
    Posts
    669

    Re: Question about Array

    The line:
    VB Code:
    1. 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:
    1. Dim ZipArray(4) As String

    All things in .NET are zero-based (I believe.)


    Hope it helps!
    Using Windows XP Home sp3
    Mucking around with C# 2008 Express
    while ( this.deadHorse ) { flog( ); }


  5. #5
    Member
    Join Date
    Feb 2006
    Posts
    49

    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

  6. #6
    Frenzied Member conipto's Avatar
    Join Date
    Jun 2005
    Location
    Chicago
    Posts
    1,175

    Re: Question about Array

    When declaring fixed arrays like that, it's easier to use this type of declaration:
    VB Code:
    1. 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
    Hate Adobe Acrobat? My Codebank Sumbissions - Easy CodeDom Expression evaluator: (VB / C# ) -- C# Scrolling Text Display

    I Like to code when drunk. Don't say you weren't warned.

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: Question about Array

    Quote 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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  8. #8
    Member
    Join Date
    Feb 2006
    Posts
    49

    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

  9. #9
    Frenzied Member conipto's Avatar
    Join Date
    Jun 2005
    Location
    Chicago
    Posts
    1,175

    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
    Hate Adobe Acrobat? My Codebank Sumbissions - Easy CodeDom Expression evaluator: (VB / C# ) -- C# Scrolling Text Display

    I Like to code when drunk. Don't say you weren't warned.

  10. #10

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2005
    Posts
    259

    Re: Question about Array

    Thanks. Both changing the original Dim Array to 4 and Dimming the array like this worked.
    VB Code:
    1. Dim ZipArray() As String = { _
    2.             "49015", _
    3.             "49418", _
    4.             "49001", _
    5.             "49002", _
    6.             "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
  •  



Click Here to Expand Forum to Full Width