I'm converting a working, VB6 project to VB.NET and having some problems
with arrays. I can't easily find any info on how Arrays have changed in .NET
and how to get them to work.
I have the following code:
Public Class Form1
Inherits System.Windows.Forms.Form
Public ImgArray()
+ Windows Form Designer generated code
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
ImgArray(1) = 1
End Sub
End Class
I don't get any build errors, but when running the project with debugging, I
get the following error dialog:
"An unhandled exception of type "System.NullReferenceException" occured in
arraytest.exe Additional information: Object reference not set to an
instance of an object."
If I run the project without debugging I get:
"An unhandled exceptin has occured in you application..... Object reference
not set to an instance of an object.
Ok, so I understand that perhaps the Array needs to be instantiated, but how
do I do that?
If anyone can point me to some good online VB.Net references and tutorials
I'd appreciate it.
Thanks,
James
You have to declare the type. Public ImgArray() as integer (or
whatever it is).
Ken
--------------------
"James Johnson" <ja...@smb-studios.com> wrote in message
news:%23TOEDsy...@TK2MSFTNGP11.phx.gbl...
I tried that and I still get the same error message.
Public Class Form1
Inherits System.Windows.Forms.Form
Public ImgArray() As String <--- new
+ Windows Form Designer generated code
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
ImgArray(1) = "1" <--- new
End Sub
End Class
Frustrating. It works fine in VB6
James
"Ken Tucker" <vb...@bellsouth.net> wrote in message
news:uVXjgwyO...@TK2MSFTNGP10.phx.gbl...
"James Johnson" <ja...@smb-studios.com> schrieb:
> I tried that and I still get the same error message.
> Public Class Form1
> Inherits System.Windows.Forms.Form
> Public ImgArray() As String <--- new
>
> + Windows Form Designer generated code
>
> Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
> System.EventArgs) Handles MyBase.Load
> ImgArray(1) = "1" <--- new
> End Sub
> End Class
>
> Frustrating. It works fine in VB6
No, it will not work in VB6.
Replace
Public ImgArray() As String
with
Public ImgArray(0) As String .
Then you can access the first element in the array with
ImgArray(0) = "1" .
Regards,
Herfried K. Wagner
--
MVP · VB Classic, VB .NET
http://www.mvps.org/dotnet
I think I got it. I don't know what the dimensions of the array will be so,
I tried this:
Public ImgArray() As String
Then, in the Form1_load sub...
ReDim ImgArray(1)
ImgArray(1) = "foobar"
And that works.
Thanks,
James
"Herfried K. Wagner" <hirf...@m.activevb.de> wrote in message
news:esEoO%23yODH...@TK2MSFTNGP10.phx.gbl...
Sure? There must be some code that creates the array (Redim statement)
otherwise it doesn't work in VB6, too.
--
Armin
As soon as possible, you should enable Option Strict for the project, or at
least the file. Then you'll have to specify the type of the array elements,
e.g. Public ImgArray() As Object
> I have the following code:
>
> Public Class Form1
> Inherits System.Windows.Forms.Form
> Public ImgArray()
>
> + Windows Form Designer generated code
>
> Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
> System.EventArgs) Handles MyBase.Load
> ImgArray(1) = 1
> End Sub
> End Class
>
> I don't get any build errors, but when running the project with
> debugging, I get the following error dialog:
> "An unhandled exception of type "System.NullReferenceException"
> occured in arraytest.exe Additional information: Object reference not
> set to an instance of an object."
>
> If I run the project without debugging I get:
> "An unhandled exceptin has occured in you application..... Object
> reference not set to an instance of an object.
>
> Ok, so I understand that perhaps the Array needs to be instantiated,
> but how do I do that?
Redim ImgArray(2)
or
ImgArray = New Object() {1,2,3}
> If anyone can point me to some good online VB.Net references and
> tutorials I'd appreciate it.
VS 2002:
ms-help://MS.VSCC/MS.MSDNVS/vbcn7/html/vbconArrays.htm
VS 2003:
ms-help://MS.VSCC.2003/MS.MSDNQTR.2003FEB/vbcn7/html/vbconArrays.htm
--
Armin
"James Johnson" <ja...@smb-studios.com> schrieb:
> I think I got it. I don't know what the dimensions
> of the array will be so, I tried this:
Notice that ReDim creates a new array which is a copy of the old one
with appropriate size. This is costly.