From: mp on
vbnet 2008 express
how to avoid the compiler warning:
Variable 'ArrayObjects' is used before it has been assigned a value. A null
null
reference exception could result at runtime.

given
Dim ArrayObjects() As AcadEntity

....some code...

Try

ReDim ArrayObjects(0)

ArrayObjects(UBound(ArrayObjects)) = oSideLine'(oSideLine is an acad entity)

Catch ex As ApplicationException

m_Util.Logentry("error " & ex.Message)

End Try

....more code...then...

Try

If ArrayObjects Is Nothing Then <------------here is the warning location at
"ArrayObjects"

m_Util.Logentry("error ArrayObjects Is Nothing")

Else

SelectionSetWblock.AddItems(ArrayObjects)

End If

Catch ex As ApplicationException

End Try



what should I be doing in such cases (using an array)

i wouldn't think you'd do Dim ArrayObjects() As AcadEntity = Nothing

like you might with a simple object???

thanks

mark


From: Armin Zingler on
Am 07.04.2010 20:50, schrieb mp:
> vbnet 2008 express
> how to avoid the compiler warning:
> Variable 'ArrayObjects' is used before it has been assigned a value. A null
> null
> reference exception could result at runtime.
>
> given
> Dim ArrayObjects() As AcadEntity
>
> ....some code...
>
> Try
>
> ReDim ArrayObjects(0)
>
> ArrayObjects(UBound(ArrayObjects)) = oSideLine'(oSideLine is an acad entity)
>
> Catch ex As ApplicationException
>
> m_Util.Logentry("error " & ex.Message)
>
> End Try
>
> ....more code...then...
>
> Try
>
> If ArrayObjects Is Nothing Then <------------here is the warning location at
> "ArrayObjects"
>
> m_Util.Logentry("error ArrayObjects Is Nothing")
>
> Else
>
> SelectionSetWblock.AddItems(ArrayObjects)
>
> End If
>
> Catch ex As ApplicationException
>
> End Try
>
>
>
> what should I be doing in such cases (using an array)
>
> i wouldn't think you'd do Dim ArrayObjects() As AcadEntity = Nothing
>
> like you might with a simple object???

Well, what is a not-simple object? An array is an object, so you can assign
Nothing in the declaration line as you've checked that the warning isn't
justified in this case.

--
Armin
From: Mr. Arnold on
mp wrote:
>
> If ArrayObjects Is Nothing Then <------------here is the warning location at
> "ArrayObjects"
>

You only want to do something, work with the object, if it is 'NOT' null.

So you have an if statement checking for the NOT null condition.

On the other hand, if the object is never going to be null, then why
worry about? It's just a warning message.
From: mp on

"Mr. Arnold" <Arnold(a)Arnold.com> wrote in message
news:Ok%23zQRp1KHA.4724(a)TK2MSFTNGP02.phx.gbl...
> mp wrote:
>>
>> If ArrayObjects Is Nothing Then <------------here is the warning location
>> at
>> "ArrayObjects"
>>
>
> You only want to do something, work with the object, if it is 'NOT' null.
>
> So you have an if statement checking for the NOT null condition.
>
> On the other hand, if the object is never going to be null, then why worry
> about? It's just a warning message.

ok, i just thought the warning was telling me i was doing something "wrong"
and trying to learn best practices
thanks Mr Arnold and Armin


From: Phill W. on
On 07/04/2010 19:50, mp wrote:

> how to avoid the compiler warning:
> Variable 'ArrayObjects' is used before it has been assigned a value. A null
> null reference exception could result at runtime.

> Dim ArrayObjects() As AcadEntity

The compiler is warning you that this variable /might/ have no value
when you come to use it.

Avoid this by giving the variable a value - even if that value just
happens to be a /null/ one!

Dim ArrayObjects As AcadEntity() = Nothing

(I prefer the array braces with the Type, not the Variable - it just
reads better, IMHO - has the same effect either way).

OK, you /will/ still get a NullReferenceException later on if you don't
give it another value, but it'll be your all own doing. :-)

HTH,
Phill W.