check for empty entity in an Array of Entities

check for empty entity in an Array of Entities

Anonymous
Not applicable
620 Views
2 Replies
Message 1 of 3

check for empty entity in an Array of Entities

Anonymous
Not applicable

Hi,

I would like to check for a array of Acadline, if a perticular row , column is empty or not.

 

Dim aline(0 to 10, 0 to 10) as AcadLine

......

......

like if i want to check if aline(i,j) is empty or not, how do i check.

 

 

 

 

0 Likes
621 Views
2 Replies
Replies (2)
Message 2 of 3

Anonymous
Not applicable

not sure I'm answering properly your needs

 

you could set a dummy acadline variable to aline(i,j) and check whether the former is nothing or not

 

    On Error Resume Next
    Set alineTemp = aline(0, 0)
    On Error GoTo 0
    If alineTemp Is Nothing Then
    
    Else
    
    End If

 

not even sure the "On Error..." statements must be used: I put them there just to prevent code stopping should any error be risen by the "set" statement

 

0 Likes
Message 3 of 3

norman.yuan
Mentor
Mentor

In classical VB/VBA, a variable is always initialzed upon its declaration. A variable referred to an Object would be initialized to Nothing:

 

So,

 

Dim obj as Object

 

means obj is set to Nothing at its declaration

 

So, if you declare an array of given type, then each of the element of the array is initialized. In your case,

 

Dim aline(0 to 10, 0 to 10) as AcadLine

 

all the 121 elements are initialized to Nothing.

 

Following code proves this:

 

 

Public Sub Test()

    Dim objs(0 To 2) As AcadEntity
    Dim ent As AcadEntity
    Dim i As Integer
    
    For i = 0 To UBound(objs)
        Set ent = objs(i)
        If ent Is Nothing Then
            MsgBox "Entity " & i & ": NOTHING"
        Else
            ''This line of code would never run
            MsgBox "Entity " & i & ": good"
        End If
    Next
    
End Sub

 

Since you may have some code run after the Dim... and some of the elements may get assigned to actual AcadLine objects, so, you simply test the element in interest to see if it is Nothing:

 

If aline(i,j) Is Nothing Then

    ''Do something

Else

    ''Do something else

End if 

Norman Yuan

Drive CAD With Code

EESignature

0 Likes