TheSwamp
Code Red => .NET => Topic started 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--
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
}
}
-
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
-
Try This:
SelectionSet sset = null; // <--
-
SelectionSet sset = null; // <--
You'd have to pay with a (sset != null) test later.
Better fix the control flow, i.e. replace
ssget = ed.GetSelection(); // Prompt for selection
if (ssget.Status == PromptStatus.OK)
{
sset = ssget.Value;
}
with
ssget = ed.GetSelection(); // Prompt for selection
if (ssget.Status != PromptStatus.OK) return;
sset = ssget.Value;
-
Yup I agree, the code needs a few small changes........
This worked for me:
[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)
-
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 !!
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
}
}