Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Here is my visual basic code

VB
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
Posted

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
VB
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
 
Share this answer
 
v2
Comments
Bensingh 27-Sep-15 6:55am    
Can you please explain this with my above code please how to implement
Wendelius 27-Sep-15 7:27am    
You're only redimensioning the array in the code so I don't know how you use the array but have a look at the modified solution.
Maciej Los 27-Sep-15 7:22am    
Good point, a5!
Please, see my answer.
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 that ReDim W(N-2 to N+2) statement creates new array and destoroys original one. If you want to save original array, use Preserve keyword.

As Mika Wendelius[^] mentioned, you can use Dictionary 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)[^]
 
Share this answer
 
v2
Comments
Wendelius 27-Sep-15 7:29am    
Good points!

By the way, just to make sure, did you get my email?
Maciej Los 27-Sep-15 9:47am    
Thank you, Mika.
Yeah, i haven't enough time to reply you, but i'll do.
Bensingh 27-Sep-15 10:26am    
Thanks a lot
Maciej Los 27-Sep-15 11:29am    
You're very welcome.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900