Author Topic: a "return" inside an "if" (C#)  (Read 3167 times)

0 Members and 1 Guest are viewing this topic.

A_LOTA_NOTA

  • Guest
a "return" inside an "if" (C#)
« on: January 09, 2009, 10:44:31 AM »
Is it possible to have a “return” inside an “if”? I have pasted below what I’m trying to do. Maybe I’m going about it all wrong but I know someone here can set me strait.

Code: [Select]
   // Was something selected?
            if (ssResult.Status != PromptStatus.OK)
            {
                ed.WriteMessage("\nNothing was selected");
            }
            // Something was selected
            else
            {
                ObjectId[] objIds = ssResult.Value.GetObjectIds();
                return objIds;
            }

T.Willey

  • Needs a day job
  • Posts: 5251
Re: a "return" inside an "if" (C#)
« Reply #1 on: January 09, 2009, 10:58:29 AM »
Yes, but you need to have a return if nothing was selected.  So either in the first portion of the if statement, or after the if statement is executed.
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

A_LOTA_NOTA

  • Guest
Re: a "return" inside an "if" (C#)
« Reply #2 on: January 09, 2009, 11:03:16 AM »
Yes, but you need to have a return if nothing was selected.  So either in the first portion of the if statement, or after the if statement is executed.

So somthing like this should work?

Code: [Select]
// Was something selected?
            if (ssResult.Status != PromptStatus.OK)
            {
                ed.WriteMessage("\nNothing was selected");
                return new ObjectId[] {};
            }
            // Something was selected
            else
            {
                ObjectId[] objIds = ssResult.Value.GetObjectIds();
                return objIds;
            }

T.Willey

  • Needs a day job
  • Posts: 5251
Re: a "return" inside an "if" (C#)
« Reply #3 on: January 09, 2009, 11:05:27 AM »
Assuming that your function returns an ObjectId[], then yes.
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

A_LOTA_NOTA

  • Guest
Re: a "return" inside an "if" (C#)
« Reply #4 on: January 09, 2009, 11:08:56 AM »
Assuming that your function returns an ObjectId[], then yes.

Yes it does & yes it works!

Thanks for your help!

T.Willey

  • Needs a day job
  • Posts: 5251
Re: a "return" inside an "if" (C#)
« Reply #5 on: January 09, 2009, 11:17:38 AM »
You're welcome.  With C#, you have to make sure that each path the code can take will return the type defined by the function.
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

A_LOTA_NOTA

  • Guest
Re: a "return" inside an "if" (C#)
« Reply #6 on: January 09, 2009, 01:05:36 PM »
You're welcome.  With C#, you have to make sure that each path the code can take will return the type defined by the function.

Thanks for your help Tim. I knew someone would point me in the right direction. It is nothing spectacular just a place to start learning.

Code: [Select]
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Runtime;

namespace My_AutoCAD_Managed_CS_Project_Application1
{
    public class GetEntity
    {
        [CommandMethod("Test")]
        public static void Testing()
        {
            Document doc = Application.DocumentManager.MdiActiveDocument;
            Transaction tr = doc.TransactionManager.StartTransaction();

            ObjectId entity = GetSingleEntity();

            var ent = (Entity)tr.GetObject(entity, OpenMode.ForWrite);
            ent.ColorIndex = 1;
            tr.Commit();
           
        }// end Testing


        [CommandMethod("GetEntity")]
        public static ObjectId GetSingleEntity()
        {
            Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;

            //Create a new entity selection options object
            var entitySelectionOpts = new PromptEntityOptions("\nSelect Object: ");

            //Start the selection process
            PromptEntityResult entitySelectionResult = ed.GetEntity(entitySelectionOpts);

            // Was something selected?...
            if (entitySelectionResult.Status != PromptStatus.OK)
            {
                ed.WriteMessage("\nNothing was selected");
                return new ObjectId();
            }
                // Something selected, so...
            else
            {
                ObjectId objIds = entitySelectionResult.ObjectId;
                return objIds;
            }
        }// end GetSingleEntity
    }// end class
}// end namespace

Glenn R

  • Guest
Re: a "return" inside an "if" (C#)
« Reply #7 on: January 12, 2009, 11:25:31 AM »
I would change your return statement from this:

Code: [Select]
return new ObjectId();

to this:

Code: [Select]
return ObjectId.Null;

That is the way to signify a null or invalid ObjectId.