Author Topic: function return value  (Read 2536 times)

0 Members and 1 Guest are viewing this topic.

jsr

  • Guest
function return value
« on: June 27, 2011, 03:07:38 AM »
I am writing a function with following code.

Code: [Select]

public static ObjectId PickAnEntity(string msg, string rejectMsg, Type [] entType)
        {
            PromptEntityOptions pEo = new PromptEntityOptions(msg);

            pEo.SetRejectMessage(rejectMsg);

       
            foreach(Type type in entType)
            {
                pEo.AddAllowedClass(type, true);
            }

            PromptEntityResult pEr = HelperClass.GetEditor().GetEntity(pEo);

            if (pEr.Status == PromptStatus.OK)
            {
                return pEr.ObjectId;
            }
            else
            {
                return new ObjectId(0);
            }

          }


       
 Now if selection status is OK I return the object id but in case of some problem (such as if ESC is pressed) what value to

 return from else portion. If I return { return new ObjectId(0); } I get following message which I do not understand
 
 completely.


'Autodesk.AutoCAD.DatabaseServices.ObjectId.ObjectId(int)' is obsolete: 'Use ObjectId(IntPtr)'   

Please Guide.

Thanks.

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: function return value
« Reply #1 on: June 27, 2011, 03:25:31 AM »
Hi,

You can use the 'Null' static property:

Code: [Select]
return ObjectId.Null;
Speaking English as a French Frog

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: function return value
« Reply #2 on: June 27, 2011, 04:44:48 AM »

or if you want numeric zero,

IntPtr.Zero
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

jsr

  • Guest
Re: function return value
« Reply #3 on: June 27, 2011, 06:32:43 AM »
Thank for the replies I do not understand "IntPtr.Zero". Please explain.

Thanks again.

kaefer

  • Guest
Re: function return value
« Reply #4 on: June 27, 2011, 06:54:23 AM »
Thank for the replies I do not understand "IntPtr.Zero". Please explain.

Doc sez:
Quote
A read-only field that represents a pointer or handle that has been initialized to zero.

Liberal use of pointers may smell like unsafe code. While there's no tangible difference in this case, I'd suggest using System.Nullable as a safe way to indicate the null condition. Works with other data types that may not have a null representation too. Viz:

Code: [Select]
        public static ObjectId? PickAnEntity(string msg, string rejectMsg, Type[] entType)
        {
            PromptEntityOptions pEo = new PromptEntityOptions(msg);
            ObjectId? oid = null;

            pEo.SetRejectMessage(rejectMsg);

            foreach (Type type in entType)
            {
                pEo.AddAllowedClass(type, true);
            }

            PromptEntityResult pEr = HelperClass.GetEditor().GetEntity(pEo);

            if (pEr.Status == PromptStatus.OK)
            {
                oid = pEr.ObjectId;
            }
            return oid;
        }

fixo

  • Guest
Re: function return value
« Reply #5 on: June 30, 2011, 09:44:37 AM »
Good work, thanks
Next one must be interesting  too I hope
I saw somewhere in good code practic that is good
when the function return boolean and value of
variable referenced with out
Thorsten will be beat me for that :)
What do you think?

Code: [Select]
        public static bool PickAnEntity(Editor ed, string msg, string rejectMsg, Type[] entType, out ObjectId oid)
        {
            PromptEntityOptions pEo = new PromptEntityOptions(msg);

            pEo.SetRejectMessage(rejectMsg);

            foreach (Type type in entType)
            {
                pEo.AddAllowedClass(type, true);
            }

            PromptEntityResult pEr = ed.GetEntity(pEo);

            if (pEr.Status == PromptStatus.OK)
            {
                oid = pEr.ObjectId;

                return true;
            }
            else
            {
                oid = ObjectId.Null;
                return false;
            }

        }
        [CommandMethod("SEE")]
        public void TryIt()
        {
            Document doc = Application.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            Editor ed = doc.Editor;
            using (Transaction trans = db.TransactionManager.StartTransaction())
            {
                ObjectId id = new ObjectId();
                if (PickAnEntity(ed, "Select a line or polyline >>", "\nWrong oBject type selected\n", new Type[] { typeof(Line), typeof(Polyline) }, out id))
                {
                    Application.ShowAlertDialog(string.Format("Good boy, get a beer\nObjectId = {0}\nType is {1}",id,(DBObject)trans.GetObject(id,OpenMode.ForRead)));
                }

            }
        }
« Last Edit: July 01, 2011, 04:57:39 PM by fixo »

Bryco

  • Water Moccasin
  • Posts: 1883
Re: function return value
« Reply #6 on: June 30, 2011, 07:36:17 PM »
out ObjectId oid is often less work than ref

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8718
  • AKA Daniel
Re: function return value
« Reply #7 on: June 30, 2011, 10:47:53 PM »
If the object id is not allowed to be null, you can always throw an exception

Jeff H

  • Needs a day job
  • Posts: 6150
Re: function return value
« Reply #8 on: June 30, 2011, 10:51:35 PM »
For if the user presses escape you can check
HostApplicationServices.Current.UserBreak

fixo

  • Guest
Re: function return value
« Reply #9 on: July 01, 2011, 04:59:18 PM »
out ObjectId oid is often less work than ref
Sorry Bryco, my bad :)
I was writing this without Cad from memory
Thank you, I edited my bad code
Regards,
Oleg

kaefer

  • Guest
Re: function return value
« Reply #10 on: July 03, 2011, 01:16:08 PM »
What do you think?

Many roads lead to Rome. Looking at it from a F# perspective, where we have the Option type, I thought System.Nullable and C#2.0's ? operator as its closest aquivalent.

Here are examples of the alternative approaches, note in each case the avoidance of pre-assignment of the null value, as per the language philosophy to greatly reduce the use of null literals.

Code: [Select]
module Null01

open Autodesk.AutoCAD.ApplicationServices
open Autodesk.AutoCAD.EditorInput
open Autodesk.AutoCAD.DatabaseServices
open Autodesk.AutoCAD.Geometry
open Autodesk.AutoCAD.Runtime

open System.Runtime.InteropServices

let pickAnEntityCommon msg rejectMsg entType =
    let ed = Application.DocumentManager.MdiActiveDocument.Editor
    let pEo = new PromptEntityOptions(msg)
    pEo.SetRejectMessage rejectMsg
    for typ in entType do
        pEo.AddAllowedClass(typ, true)
    ed.GetEntity pEo

// Using type-specific out-of-band value, returns ObjectId
let pickAnEntity1 msg rejectMsg entType =
    let pEr = pickAnEntityCommon msg rejectMsg entType
    if pEr.Status = PromptStatus.OK then
        pEr.ObjectId
    else
        ObjectId.Null

// Using option<'T>, returns ObjectId option
let pickAnEntity2 msg rejectMsg entType =
    let pEr = pickAnEntityCommon msg rejectMsg entType
    if pEr.Status = PromptStatus.OK then
        Some pEr.ObjectId
    else
        None

// Using out parameter, not recommended except for interoperability with other CLI languages
// Returns bool
type Foo() =
    static member PickAnEntity3(msg, rejectMsg, entType, [<Out>]res: byref<ObjectId>) =
        let pEr = pickAnEntityCommon msg rejectMsg entType
        if pEr.Status = PromptStatus.OK then
            res <- pEr.ObjectId
            true
        else
            false

[<CommandMethod "SEE">]
let tryIt() =
    let ed = Application.DocumentManager.MdiActiveDocument.Editor
    let msg = "Select a line or polyline >>"
    let rejectMsg = "Wrong oBject type selected"
    let entType = [ typeof<Line>; typeof<Polyline> ]
    let succeed (oid: ObjectId) =
        Application.ShowAlertDialog(
            System.String.Format("Good boy, get a beer\nObjectId = {0}\nType is {1}", oid, oid.ObjectClass.Name) )
   
    // Check for special value
    let oid = pickAnEntity1 msg rejectMsg entType
    if not oid.IsNull then succeed oid
           
    // Pattern matching against option<'T>
    match pickAnEntity2 msg rejectMsg entType with
    | Some oid -> succeed oid
    | None -> () // Do nothing

    // Compiler converts byref arguments from the end of the argument tuple into return tuple
    match Foo.PickAnEntity3(msg, rejectMsg, entType) with
    | true, oid -> succeed oid
    | false, _ -> () // Do nothing

Cheers, Thorsten