|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
null reference exception could result at runtimehow 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 Am 07.04.2010 20:50, schrieb mp:
Show quoteHide quote > vbnet 2008 express Well, what is a not-simple object? An array is an object, so you can assign> 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??? Nothing in the declaration line as you've checked that the warning isn't justified in this case. -- Armin mp wrote:
> You only want to do something, work with the object, if it is 'NOT' null.> If ArrayObjects Is Nothing Then <------------here is the warning location at > "ArrayObjects" > 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.
Show quote
Hide quote
"Mr. Arnold" <Arn***@Arnold.com> wrote in message ok, i just thought the warning was telling me i was doing something "wrong" news:Ok%23zQRp1KHA.4724@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. and trying to learn best practices thanks Mr Arnold and Armin On 07/04/2010 19:50, mp wrote:
> how to avoid the compiler warning: The compiler is warning you that this variable /might/ have no value > Variable 'ArrayObjects' is used before it has been assigned a value. A null > null reference exception could result at runtime. > Dim ArrayObjects() As AcadEntity 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. Thanks, I'd seen declarations setting to nothing with "regular objects", but
was thinking an array was an array, not an object... It was cleared up for me that arrays are objects in dot net...i'm slowly getting that through my thick skull that everyting is an object in dot net. Thanks for your response Mark Show quoteHide quote "Phill W." <p-.-a-.-w-a-r-d-@-o-p-e-n-.-a-c-.-u-k> wrote in message news:hpki8s$4vm$1@south.jnrs.ja.net... > 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.
How to get the list of all SQL Server foreign keys in a database by using Microsoft.SqlServer.Manage
Problem passing exception from AppDomain want to convert string into byte array hw i can do it datagridview Combobox column Adding/copying Worksheet to Excel Programmatically add Sub Menus to a ContextMenuStrip HTML report in vb.net master - detail architecture for dataTable. textBox databinding. Sorry for reposting - can not figure out... |
|||||||||||||||||||||||