Google 网上论坛不再支持新的 Usenet 帖子或订阅项。历史内容仍可供查看。

Arrays -- Help please

已查看 2 次
跳至第一个未读帖子

James Johnson

未读,
2003年6月25日 11:04:092003/6/25
收件人
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

未读,
2003年6月25日 11:13:132003/6/25
收件人
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

未读,
2003年6月25日 11:26:472003/6/25
收件人
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

未读,
2003年6月25日 11:36:332003/6/25
收件人
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

未读,
2003年6月25日 11:46:182003/6/25
收件人
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

未读,
2003年6月25日 11:49:352003/6/25
收件人
"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

未读,
2003年6月25日 11:43:242003/6/25
收件人
"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

未读,
2003年6月25日 12:16:192003/6/25
收件人
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

未读,
2003年6月25日 12:20:372003/6/25
收件人
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 个新帖子