Reurning Arrays

  • Thread starter Thread starter HardySpicer
  • Start date Start date
H

HardySpicer

I have a function which returns an array of strings. I want to display
say the zeroth element and I do

mystring = myarrayofstrings(0)

which is it happy with. However, when i compile it complains twith
this error

System.NullReferenceException was unhandled
Message="Object reference not set to an instance of an object."

Then it says to use the new keyword to create an object instance!

What does that all mean?

Hardy
 
HardySpicer said:
I have a function which returns an array of strings. I want to
display say the zeroth element and I do

mystring = myarrayofstrings(0)

which is it happy with. However, when i compile it complains twith
this error

System.NullReferenceException was unhandled
Message="Object reference not set to an instance of an object."

Then it says to use the new keyword to create an object instance!

What does that all mean?


How do you return the array from the function?


Armin
 
You say when you compile.... You mean when you run it?

It means that either myarrayofstrings or myarrayofstrings(0) is 'Nothing'.
Better check the function. Remember that myarrayofstrings and
myarrayofstrings(0) are 'pointers'. This is the error you get when you try
to use a null reference.
 
You should investigate your myarrayofstrings function as it is obviously not
returning a valid array of strings during runtime

also make sure you have
option strict on and option explicit on this will tackle most of the
common problems like these

example :

dim MyArr () as string

msgbox (Myarr(0))

the above wil compile fine when option strict is off but will throw an error
during runtime

HTH

Michel
 
Back
Top