Author Topic: Testing if a SelectionSet is empty  (Read 2664 times)

0 Members and 1 Guest are viewing this topic.

ZNETQ

  • Guest
Testing if a SelectionSet is empty
« on: November 08, 2011, 09:37:38 AM »
I'm trying to test if a SelectionSet is empty. I've got a compiler error: "Use of unassigned local variable", but I can't see what I'm supposed to do. See this short code: --help !!--thanks--
Code: [Select]
static public void usr_select()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Editor ed = doc.Editor;
SelectionSet sset; // <--------------------------------------
PromptSelectionResult ssget;
ssget = ed.SelectImplied(); // Get the PickFirst selection set
if (ssget.Status == PromptStatus.OK)
{
sset = ssget.Value;
}
        else
{
ssget = ed.GetSelection(); // Prompt for selection
if (ssget.Status == PromptStatus.OK)
{
sset = ssget.Value;
}
}
if (sset.Count > 0) // Error: Use of unassigned local variable 'sset'
{
              // main program
}
}

kaefer

  • Guest
Re: Testing if a SelectionSet is empty
« Reply #1 on: November 08, 2011, 09:50:54 AM »
Code: [Select]
ssget = ed.GetSelection(); // Prompt for selection
if (ssget.Status == PromptStatus.OK)
{
sset = ssget.Value;
}

Hi ZNETQ,

welcome to the Swamp.

Your variable could be unassigned when your prompt quoted above returns with ssget.Status != PromptStatus.OK. It would be customary in that case to return early, since normally the cause would be either an error or that the user issued a cancel. In your case it could also mean that nothing was selected (i.e. PromptStatus.None).

Cheers

RMS

  • Guest
Re: Testing if a SelectionSet is empty
« Reply #2 on: November 08, 2011, 12:34:50 PM »
Try This:

Code: [Select]
SelectionSet sset = null; // <--

kaefer

  • Guest
Re: Testing if a SelectionSet is empty
« Reply #3 on: November 08, 2011, 01:53:12 PM »
Code: [Select]
SelectionSet sset = null; // <--

You'd have to pay with a (sset != null) test later.

Better fix the control flow, i.e. replace
Code: [Select]
                ssget = ed.GetSelection(); // Prompt for selection
                if (ssget.Status == PromptStatus.OK)
                {
                    sset = ssget.Value;
                }
with
Code: [Select]
                ssget = ed.GetSelection(); // Prompt for selection
                if (ssget.Status != PromptStatus.OK) return;
                sset = ssget.Value;

RMS

  • Guest
Re: Testing if a SelectionSet is empty
« Reply #4 on: November 08, 2011, 03:23:56 PM »
Yup I agree, the code needs a few small changes........

This worked for me:

Quote
        [CommandMethod("selectss", CommandFlags.UsePickSet)]
        public static void CheckForPickfirstSelection()
        {

            Document doc = Application.DocumentManager.MdiActiveDocument;
            Editor ed = doc.Editor;
            SelectionSet sset = null; // <--------------------------------------   
            PromptSelectionResult ssget;
            ssget = ed.SelectImplied(); // Get the PickFirst selection set
            if (ssget.Status == PromptStatus.OK)
            {
                sset = ssget.Value;
            }
            else
            {
                ssget = ed.GetSelection(); // Prompt for selection
                if (ssget.Status == PromptStatus.OK)
                {
                    sset = ssget.Value;
                    // main program
                    //Objects Found
                }
                else
                {
                    //No Object Found
                }

            }
        }

see also:
http://docs.autodesk.com/ACD/2010/ENU/AutoCAD%20.NET%20Developer's%20Guide/index.html

ZNETQ

  • Guest
Re: Testing if a SelectionSet is empty
« Reply #5 on: November 09, 2011, 03:03:14 AM »
Hi kaefer & RMS, thanks very much !
"Initializing" sset to null is exactly what I was looking for.
Indeed a premature return would make the code simpler but due to other considerations this program is better off with a unique exit point.

Problem solved !!
Code: [Select]
static public void usr_select()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Editor ed = doc.Editor;
SelectionSet sset = null;
PromptSelectionResult ssget;
ssget = ed.SelectImplied(); // Get the PickFirst selection set
if (ssget.Status == PromptStatus.OK)
{
sset = ssget.Value;
}
        else
{
ssget = ed.GetSelection(); // Prompt for selection
if (ssget.Status == PromptStatus.OK)
{
sset = ssget.Value;
}
}
if (sset != null)
{
              // main program
}
}