Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Bogus arraylengths in VB.Net?

Options
  • 07-06-2003 3:26pm
    #1
    Closed Accounts Posts: 413 ✭✭


    I've been playing with .Net for the past week, using both VB.Net and C#. I'm overall quite impressed, it's seems to be a pretty powerful environment.

    However, I ran into this evil piece of language design:

    Dim i(10) As Integer

    gives an array of 11 elements. I mean, Christ on a bike. I want whatever that coder was smoking, so I can kill herds of elephants with it.

    Now, believe it or not, I'm going to remain optimistic - I've been using a Beta release - beta 2, before I buy a proper copy, so can someone confirm that that's been fixed? I'd sleep better at night. (</sarcasm>)

    Much appreciated,
    Chris.


Comments

  • Registered Users Posts: 15,443 ✭✭✭✭bonkey


    IIRC, in BASIC, arrays were defined from 1 by default. Therefore, to a BASIC programmer, if you wanted a 10-element array, you did a dim i(10) as integer.

    Somewhere along the line (possibly VB1, but I think it was even later), MS gave in to pressure from "real" programmers who laughed at BASIC and VB for such silly things as not starting a count from 0. Obviously such languages were only for people who couldnt handle any slightly complex concepts......and it recurses from there.

    Anyway...they decided to shift the definition from 1-based to 0-based, to try and gain credibility...but for some reason stuck with the old upper-limit definitions.

    I'm amazed it made it into VB.NET, but it did. Not only that, but they got rid of the Option Base option from VB 6 and previous which let you define what the arrays started from...


    jc


  • Closed Accounts Posts: 9,314 ✭✭✭Talliesin


    It's worth remarking on the intermeditary period (IIRC you can't do this with VB.Net, though you can with VB6) in which you could have:
    Dim i(1 To 10) As Integer
    
    or
    Dim i(0 To 9) As Integer
    
    or even
    Dim i(120 To 129) As Integer
    
    All of which have 10 elements, though with different lower bounds. As such the code
    Dim i(10) as Integer
    
    was a shorthand which ommited the lower bound, the lower bound's default would depend on what was set using the Option statement, and could be either 1 or 0.

    Fixing the lower bound at 0 made things a lot more straight-forward.


  • Closed Accounts Posts: 413 ✭✭sobriquet


    [...]the lower bound's default would depend on what was set using the Option statement, and could be either 1 or 0.
    And there's the problem - in VB.Net, the lower bound is always zero, but the supplied declaration value will represent the upper-bound, thus giving off-by-one problem.

    It is mind boggling though, be it a failure of the design (though the .Net MSDN docs don't agree - it says i(10) gives a 10-element array), or an oversight in the implementation.

    Ho-hum, I'm more inclined toward C# over VB.Net anyway - though job demands may make it compulsory (euch).


Advertisement