是否有可能以负值vb开始一个数组.网 [英] is it possible to start an array with negative value vb. net
本文介绍了是否有可能以负值vb开始一个数组.网的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我的视觉基本代码
Here is my visual basic code
Sub info()
If False=0 Then
ReDim W(-2 to N+2)
End If
this Coding working in vb 6.0
But the same coding transferred to vb.net it shows an error Array lower bound to be 0
谁能指导我如何解决此问题?
Can any one please guide me how to fix this issue
推荐答案
数组不能具有负索引.据我所知,您有三个选择:
-修改代码以使用基于0的数组
-使用基于0的索引,但根据情况使用索引减法/加法2,
-使用字典(TKey,TValue)([ ^ ]
一些例子
An array cannot have a negative index. So as far as I can see you have three options:
- modify the code to use 0 based arrays
- use 0 based indexing but when using the index subtract/add 2, depending on the situation.
- use a Dictionary(Of TKey, TValue) Class[^]
Some examples
Dim N1 As Integer = 100
Dim W1(0) As Integer
ReDim W1(0 To N1 + 4)
For counter As Integer = 0 To 104
W1(counter) = counter
Next
Dim N2 As Integer = 100
Dim W2(0) As Integer
ReDim W2(0 To N2 + 4)
For counter As Integer = -2 To 102
W2(counter + 2) = counter
Next
Dim W3 As System.Collections.Generic.Dictionary(Of Integer, Integer)
W3 = New Dictionary(Of Integer, Integer)
For counter As Integer = -2 To 102
W3.Add(counter, counter)
Next
Vb6与VB.Net不同.它们之间有很多差异.我建议阅读以下内容:将Visual Basic 6.0应用程序升级到Visual Basic .NET和Visual Basic 2005 [ ^ ]并执行以下操作: 对Visual Basic和Visual Basic .NET的比较 [ Redim指令 [ ^ ]通常用于为数组变量重新分配存储空间.您可以增加/减少数组的大小,但是要记住,数组的低索引不能为负,因为数组是基于零的,除非您使用 ^ ].请注意,ReDim W(N-2 to N+2)
语句创建新数组并破坏原始数组.如果要保存原始数组,请使用Preserve
关键字.
作为米卡·温德留斯 [ Visual Basic [在Visual Basic .NET中创建类 [在Visual Basic .NET中使用类和结构 [ ^ ]
演练:定义类(Visual Basic) [
Vb6 is not the same as VB.Net. There''s a lot of differencies between them. I''d suggest to read this: Upgrading Visual Basic 6.0 Applications to Visual Basic .NET and Visual Basic 2005[^] and this: omparison of Visual Basic and Visual Basic .NET[^] to find out.
You did not provide information about what you want to achieve, but Redim instruction[^] is usually used to reallocate storage space for an array variable. You can increase/decrease the size of array, but you need to remember that low index of array cannot be negative, because arrays are zero based, unless you use Option Base 1 statement[^]. Note thatReDim W(N-2 to N+2)
statement creates new array and destoroys original one. If you want to save original array, usePreserve
keyword.
As Mika Wendelius[^] mentioned, you can useDictionary
class, but this is not the only way. Depending on what you want to achieve, you can use custom class to manage data.
For further information, please see:
Visual Basic[^]
Creating Classes in Visual Basic .NET[^]
Using Classes and Structures in Visual Basic .NET[^]
Walkthrough: Defining Classes (Visual Basic)[^]
这篇关于是否有可能以负值vb开始一个数组.网的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文