check array size when variable = nothing

Y

yhlove

Hi

I declared an array as follows:
dim a as integer()

somewhere else in my program I have the following command:
redim preserve a(size)

I want to check the array length like that
if a.length > 1 then
.......
end if

the problem is that if I check the array length before the redim
command I get an exception:
"Object reference not set to an instance of an object."
abd thats because the array doesn't have length yet (not even 0)

so what should I do? how can check if array is equal to nothing such
in this case?
I tried: if a = nothing then ..... but it has a
compilation error

Yhlove
 
G

Guest

(e-mail address removed) wrote in @w5g2000hsg.googlegroups.com:
Hi

I declared an array as follows:
dim a as integer()

somewhere else in my program I have the following command:
redim preserve a(size)

I want to check the array length like that
if a.length > 1 then
.......
end if


if not a is nothing andalso a.length > 0 then
...
end if

a is nothing = null.
 
M

Michel Posseth [MCP]

Well IMHO

you would better use this syntax

Dim a() As Integer = Array.CreateInstance(GetType(Integer), 0)

MsgBox(a.Length)

Array.Resize(a, 2)

MsgBox(a.Length)


HTH

Michel Posseth
 
C

Chris Dunaway

Well IMHO

you would better use this syntax

Dim a() As Integer = Array.CreateInstance(GetType(Integer), 0)

Why would you use the CreateInstance method instead of this:

Dim a(0) As Integer

This seems more readable to me.

Array.Resize(a, 2)

I agree with the use of Array.Resize. It seems more readable to me
than ReDim.

Chris
 
S

Stephany Young

There are subtleties as to how you 'size' an array.

Dim _a(0) As Integer
or
Dim _a() As Integer = New Integer(0) {}

both instantiate an array of integer with 1 element (index 0). That is,
interrogating _a.length will return 1.

Dim a() As Integer = Array.CreateInstance(GetType(Integer), 0)

instantiates an array of integer with 0 elements. That is, interrogating
_a.length will return 0.
 
C

Chris Dunaway

There are subtleties as to how you 'size' an array.

Dim _a(0) As Integer
or
Dim _a() As Integer = New Integer(0) {}

both instantiate an array of integer with 1 element (index 0). That is,
interrogating _a.length will return 1.

Dim a() As Integer = Array.CreateInstance(GetType(Integer), 0)

instantiates an array of integer with 0 elements. That is, interrogating
_a.length will return 0.

Thanks for the clarification. I was thinking that the CreateInstance
call was doing the same as the VB syntax and the 0 was the upper
bound.

Chris
 
P

Phill W.

I declared an array as follows:
dim a as integer()
if I check the array length before the redim command I get an exception:

How about

Dim a As Integer() = {}

Who said Visual Basic is the one /without/ the curly braces?

HTH,
Phill W.
 

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