Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Arrays -- Help please

2 views
Skip to first unread message

James Johnson

unread,
Jun 25, 2003, 11:04:09 AM6/25/03
to
Hello,

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


Ken Tucker

unread,
Jun 25, 2003, 11:13:13 AM6/25/03
to
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...

James Johnson

unread,
Jun 25, 2003, 11:26:47 AM6/25/03
to
Hi Ken,

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...

Herfried K. Wagner

unread,
Jun 25, 2003, 11:36:33 AM6/25/03
to
Hello,

"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


James Johnson

unread,
Jun 25, 2003, 11:46:18 AM6/25/03
to
Ok,

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...

Armin Zingler

unread,
Jun 25, 2003, 11:49:35 AM6/25/03
to
"James Johnson" <ja...@smb-studios.com> schrieb

> Hi Ken,
>
> 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

Sure? There must be some code that creates the array (Redim statement)
otherwise it doesn't work in VB6, too.


--
Armin

Armin Zingler

unread,
Jun 25, 2003, 11:43:24 AM6/25/03
to
"James Johnson" <ja...@smb-studios.com> schrieb

> 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.

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

Herfried K. Wagner

unread,
Jun 25, 2003, 12:16:19 PM6/25/03
to
Hello,

"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.

Herfried K. Wagner

unread,
Jun 25, 2003, 12:20:37 PM6/25/03
to
I have forgotten to tell you, that in some cases (the size of the array
changes very often) using an ArrayList is the better approach.
0 new messages