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.