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

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

问题描述

在声明在VB中的数组,你会永远离开零元空,并调整code,使之更加人性化?

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.

想想这样。只需要谁拥有理解和维护code很短的时间量来获得舒适与零索引的数组每个程序员。但是,如果你正在使用基于一的数组,不像那些在几乎所有其他VB.NET code发现,事实上,几乎所有其他常见的编程语言,它会在团队中每个人要长得多。他们会不断地犯错误,绊倒了,因为他们的自然假设在此准确的一种特殊情况

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.

有些人可能会认为,离开每个数组空的第一个元素将不必要的消耗额外的内存。而这显然是真实的,我认为这是上面psented一个我$ P $后面的次要原因。良好的开发人员编写code给别人看的,所以我推荐您考虑如何使你的code逻辑和可以理解的。您可以通过问这个问题是在正确的道路上。但是从长远来看,我不认为这个决定是一个很好的一个。

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,你也可以使用对于每个语句通过您的数组中的元素,其中很多人觉得更易读进行迭代。例如:

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天全站免登陆