Arrays in Visual Basic

后端 未结 5 1988
长发绾君心
长发绾君心 2020-12-12 04:34

In declaring an array in VB, would you ever leave the zero element empty and adjust the code to make it more user friendly?

This is for Visual Basic 2008

相关标签:
5条回答
  • 2020-12-12 05:05

    Who are the "users" that are going to see the array indexes? Any good developer will be able to handle a zero-indexed array and no real user should ever see them. If the user has to interact with the array, then make an actually user-friendly system for doing so (text or a 1-based virtual index or whatever is called for).

    0 讨论(0)
  • 2020-12-12 05:05

    In visual basic is it possible to declare an array starting from 1, if you find inconvenient to use a 0 based array.

    Dim array(1 to 10) as Integer
    

    It is just a matter of tastes. I use 1 based arrays in visual basic but 0 based arrays in C ;)

    0 讨论(0)
  • 2020-12-12 05:16

    No, I wouldn't do that. It seems like it might help maintainability, but that's a very short-sighted view.

    Think about it this way. It only takes each programmer who has to understand and maintain the code a short amount of time to get comfortable with zero-indexed arrays. But if you're using one-based arrays, which are unlike those found in almost all other VB.NET code, and in fact almost every other common programming language, it will take everyone on the team much longer. They'll be constantly making mistakes, tripping up because their natural assumptions aren't accurate in this one special case.

    I know how it feels. When I worked in VB 6, I loved one-based arrays. They were very natural for the type of data that I was storing, and I used them all over the place. Perfectly documentable here, because you have an explicit syntax to specify the upper and lower bounds of the array. That's not the case in VB.NET (which is a newer, but incompatible version of the Visual Basic language), where all arrays have to be zero-indexed. I had a hard time switching to VB.NET's zero-based arrays for the first couple of days. After that initial period of adjustment, I can honestly say I've never looked back.

    Some might argue that leaving the first element of every array empty would consume extra memory needlessly. While that's obviously true, I think it's a secondary reason behind the one I presented above. Good developers write code for others to read, so I commend you for considering how to make your code logical and understandable. You're on the right path by asking this question. But in the long run, I don't think this decision is a good one.

    There might be a handful of exceptions in very specific cases, depending on the type of data that you're storing in the array. But again, failing to do this across the board seems like it would hurt readability in the aggregate, rather than helping it. It's not particularly counter-intuitive to simply write the following, once you've learned how arrays are indexed:

    For i As Integer = 0 To (myArray.Length - 1)
        'Do work
    Next
    

    And remember that in VB.NET, you can also use the For Each statement to iterate through your array elements, which many people find more readable. For example:

     For Each i As Integer In myArray
         'Do work
     Next
    
    0 讨论(0)
  • 2020-12-12 05:20

    First, it is about programmer friendly, not user friendly. User will never know the code is 0-based or 1-based.

    Second, 0-based is the default and will be used more and more.

    Third, 0-based is more natural to computer. From the very element, it has two status, 0 and 1, not 1 and 2.

    I have upgraded a couple of VB6 projects to vb.net. To modify to 0-based array in the beginning is better than to debug the code a later time.

    0 讨论(0)
  • 2020-12-12 05:20
    • Most of my VB.Net arrays are 0-based and every element is used. That's usual in VB.Net and code mustn't surprise the reader. Readability is vital.
    • Any exceptions? Maybe if I had a program ported from VB6, so it used 0-based arrays with unused initial elements, and it needed a small change, I might match the pattern of the existing code. Least surprise again.
    • 99 times out of 100 the question shouldn't arise because you should be using List(Of T) rather than an array!
    0 讨论(0)
提交回复
热议问题