Visual Basic 中的数组 [英] Arrays in Visual Basic

查看:25
本文介绍了Visual Basic 中的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 VB 中声明一个数组时,您是否会将零元素留空并调整代码以使其更加用户友好?

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

这是针对 Visual Basic 2008 的

This is for Visual Basic 2008

推荐答案

不,我不会那样做.看起来可能有助于可维护性,但就是这样一个非常短视的观点.

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

这样想.每个必须理解和维护代码的程序员只需要很短的时间就能适应零索引数组.但是,如果您使用基于一个的数组,这与几乎所有其他 VB.NET 代码中的数组不同,实际上几乎所有其他常见的编程语言都不同,团队中的每个人都会花费更长的时间.他们会不断犯错,因为他们的自然假设在这个特殊情况中不准确.

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.

我知道那是什么感觉.当我在 VB 6 中工作时,我喜欢基于一个的数组.它们对于我存储的数据类型来说非常自然,我到处都在使用它们.这里完全可以记录,因为您有一个明确的语法来指定数组的上限和下限.在 VB.NET(它是较新的,但不兼容的 Visual Basic 语言版本)中,情况并非如此,其中所有数组都必须为零索引.在最初的几天里,我很难切换到 VB.NET 的从零开始的数组.经过那段最初的调整之后,老实说,我再也没有回头.

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

请记住,在 VB.NET 中,您还可以使用 For Each 语句来遍历您的数组元素,许多人认为这更具可读性.例如:

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

这篇关于Visual Basic 中的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆