;; Excerpt from http://www.caddzone.com/MTextFragmentSample.cs
static ObjectId SelectObject(string prompt, System.Type objectClass)
{
Editor e = AcadApp.DocumentManager.MdiActiveDocument.Editor;
using ( Transaction tr = e.Document.Database.TransactionManager.StartTransaction() )
{
while( true )
{
PromptEntityResult res = e.GetEntity(prompt);
if( res.Status != PromptStatus.OK )
return ObjectId.Null;
DBObject ob = tr.GetObject(res.ObjectId, OpenMode.ForRead);
if( objectClass.IsAssignableFrom(ob.GetType()) )
return res.ObjectId;
e.WriteMessage("\nInvalid selection, {0} entity expected",
RXClass.GetClass(objectClass).DxfName);
}
}
}
Nice construct .. I blinked when I first saw while( true ) though
I still blink, but hopefully soon I will understand why.
Hi Tim. The while( true ) {...} business might cause a LISP programmer to blink since LISP (or at least the dialect we use) has no control-transfer 'statements' other than (exit), which takes you to the end of your program (that is, unless you handle it with vl-catch-xxxxx functions).
In most imperative languages, there are a number of control transfer statements (e.g., 'return'; 'break' and (gasp) 'GOTO' are found C, C# and C++), and additionally 'Exit <control_loop_statement>' in VB, like 'Exit For'.
These statements allow you to exit a loop from any point within it, without the need for a potentially complex (and IMHO, confusing) looping condition, like we often see in LISP.
Let us know when you start scratching your head over try/catch/finallly