TheSwamp

Code Red => .NET => Topic started by: ZNETQ on November 08, 2011, 09:37:38 AM

Title: Testing if a SelectionSet is empty
Post by: ZNETQ 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
}
}
Title: Re: Testing if a SelectionSet is empty
Post by: kaefer 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
Title: Re: Testing if a SelectionSet is empty
Post by: RMS on November 08, 2011, 12:34:50 PM
Try This:

Code: [Select]
SelectionSet sset = null; // <--
Title: Re: Testing if a SelectionSet is empty
Post by: kaefer 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;
Title: Re: Testing if a SelectionSet is empty
Post by: RMS 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 (http://docs.autodesk.com/ACD/2010/ENU/AutoCAD%20.NET%20Developer's%20Guide/index.html)
Title: Re: Testing if a SelectionSet is empty
Post by: ZNETQ 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
}
}