LispFunction Test....CRASH

LispFunction Test....CRASH

Anonymous
Not applicable
664 Views
3 Replies
Message 1 of 4

LispFunction Test....CRASH

Anonymous
Not applicable

Why does the following code crash AutoCAD with unhandled Exception.....please be gentle...

 

Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.EditorInput

 

Public Class jb
    <LispFunction("test")> _
    Public Function jbStart(ByVal testarray As ResultBuffer) As ResultBuffer
        Dim rbfResult As ResultBuffer
        Dim jbtestarray As Array
        'jbtestarray = testarray.AsArray
        jbtestarray(0) = 1
        jbtestarray(1) = 2
        jbtestarray(2) = 3
        Dim myarg As Double
        Dim myTypeVal As TypedValue
        Dim i As Integer = 0
        While i < jbtestarray.Length
            myTypeVal = jbtestarray.GetValue(i)
            rbfResult = New ResultBuffer(New TypedValue(CInt(LispDataType.Double), myarg))
            i = i + 1
        End While
        Return rbfResult
    End Function
End Class

 

 

0 Likes
665 Views
3 Replies
Replies (3)
Message 2 of 4

chiefbraincloud
Collaborator
Collaborator
jerry.bryant wrote:

Why does the following code crash AutoCAD with unhandled Exception.....please be gentle...

 

That is really two questions...  Why does it crash, and why is it an unhandled exception.

 

It is an Unhandled exception, because you don't have any error handling code.

Put a Try/Catch block in it, and the exception text that arises will probably tell you what is happening.

 

That said, from what I see, you should be getting a warning from Visual Studio, that jbtestarray has been used before it has been assigned a value, on this line

 

jbtestarray(0) = 1

This is because you created a variable to hold an array, but that variable does not yet contain an instance of an array.

  Your code would also fail here:

 

myTypeVal = jbtestarray.GetValue(i)

because you can not set a variable expecting a TypedValue equal to a variable containing a Double.

 

and here: (well, not really fail as in error, but fail as in not give the desired result)

rbfResult = New ResultBuffer(New TypedValue(CInt(LispDataType.Double), myarg))

because MyArg has not been assigned a value, so it will always be zero, and because since you are assigning rbfResult to a New ResultBuffer each time, the returned result would only contain the last value.

 

Here's a guess at what you are actually trying to do.

<LispFunction("test")> _
  Public Function jbStart(ByVal testarray As ResultBuffer) As ResultBuffer
	Dim rbfResult As New ResultBuffer
	Try
		Dim jbtestarray() As TypedValue = testarray.AsArray	'this would fail if no arguments are passed
		Dim myarg As Double
		Dim myTypeVal As TypedValue
		Dim i As Integer = 0
		While i < jbtestarray.Length  'I would use a for/next loop here
			myTypeVal = jbtestarray(i)
			myarg = myTypeVal.Value
			rbfResult.Add(New TypedValue(CInt(LispDataType.Double), myarg))
			i = i + 1
		End While
		Return rbfResult
	Catch ex As Exception
		MsgBox(ex.Message & vbLf & ex.StackTrace)
		Return Nothing
	End Try
End Function

 which is essentially the same thing as having just one line of code

 

Return testarray

 

But I assume you are trying to figure out how to make use of this data for another purpose, so enjoy.

 

and, to prevent the code from failing if no arguments are passed (see comment in code), you should add a check at the beginning (very first line) that says 'if testarray is Nothing then Return Nothing'.

 

 

 

 

Dave O.                                                                  Sig-Logos32.png
0 Likes
Message 3 of 4

Anonymous
Not applicable

Thx,

 

This is the part that I don't understand....your reply..

"That said, from what I see, you should be getting a warning from Visual Studio (YES I AM GETTING THIS WARNING), that jbtestarray has been used before it has been assigned a value, on this line

 

jbtestarray(0) = 1

This is because you created a variable to hold an array, but that variable does not yet contain an instance of an array. "

 

Ok...I have..

 

Dim jbtestarray As Array

jbtestarray(0) = 1

 

How do I "create an instance" of the array before filling it?....I think I am havng a mental block on this one....

Thx again

0 Likes
Message 4 of 4

chiefbraincloud
Collaborator
Collaborator

There are a couple of ways...

 

Dim jbtestarray As Array basically makes an array that has no size or type.

One way to fix that, staying with the code you have, is to use the Shared CreateInstance method of the Array Class, like this:

 

Dim jbtestarray as Array = Array.CreateInstance(GetType(Double), 2) 

 

The return value of the CreateInstance method is an Instance of an Array with Type Double and Max index 2.

 

In most cases, I prefer a different approach, which gives you a Typed array with no set size in the declaration, like I did in my example code.

Dim jbtestarray() As TypedValue

 

If you were to follow that line of code with another which said jbtestarray(0) = 1, you would still get the same error, because although the Array is already Typed, it has no Size.  At this point the array either needs to be given a size manually using 'ReDim jbtestarray(2)', or you can set it equal to the return value of a function that returns an array, in which case the variable you created becomes a reference to the returned array instance, which was sized by the function you called, and which is what I did in my code by Dim jbtestarray() as TypedValue = testarray.AsArray

 

This is of course most useful when you don't know the size of the array at design time.  And also to prevent errors that would be caused by someone passing the wrong number of arguments to your LispFunction.

 

You can also Type and Size the array at the same time as declaring it, like:

 

Dim jbtestarray(2) as Double

 

If you then Follow that with a line that says jbtestarray(0) = 1, there will be no problem, because the array has already been sized and typed.

 

Hope that helps.

Dave O.                                                                  Sig-Logos32.png
0 Likes