Array Help

G

Guest

I am getting the following error when I try to write a value to my array.

An unhandled exception of type 'System.NullReferenceException' occurred in
EmpPrinting.exe
Additional information: Object reference not set to an instance of an object.

This error only occurs when I don't include an initial size in my
declareation (ex. DIM strArray( ) as String). If I declare an array with an
initial size (ex. DIM strArray(11) as String) then it works fine. In my
textbook it says you can declare and not include an initial size. Can anyone
help.

Thanks in advance.
 
M

Miro

I believe you can only DIM an array when you assign an array to it right
away.
===
Example - ( Written in notepad )

DIM strArray() as String = {"A", "B", "C" }
===
If you DIM strArray() as String
then you have created an Array but with "No Memory Buckets" to hold data.

So before you add to it you either have to ReDim or take a look at ReDim
PRESERVE

Take a look at this link http://www.startvbdotnet.com/language/arrays.aspx


Miro
 
G

Guest

you can declare an array without an initial size as your book says, but
before you use it you need to specify a size....ex

Dim str() As String

....
Dim x as integer

''user inputs an integer into x

ReDim str(x)

you need to make the array have a size before you can use it. hope this helps
 
H

Herfried K. Wagner [MVP]

Miro said:
I believe you can only DIM an array when you assign an array to it right
away.
===
Example - ( Written in notepad )

DIM strArray() as String = {"A", "B", "C" }
===
If you DIM strArray() as String
then you have created an Array but with "No Memory Buckets" to hold data.

You have not created an array here. You just created a variable of type
'String()' (array of strings) which contains a 'Nothing' reference by
default as array types are reference types.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top