I've just set up a bit of code to try and make things neater, but all I've really done, is add a whole bunch of nested if
statements:
public void Copy(Copy copyType)
{
FrmProjectChooser frm = null;
if (copyType.HasFlag(Copy.Single))
{
if (copyType.HasFlag(Copy.Estimations) && copyType.HasFlag(Copy.SubItems))
{
// Single item, with sub items and estimations
List<WB> items = new List<WB> { (WB)treeWBS.GetDataRecordByNode(treeWBS.FocusedNode) };
items.AddRange(_WBSManager.GetAllChildren(items[0].idWBS));
List<CTREstimation> estimations = new List<CTREstimation>();
foreach (WB wbs in items)
estimations.AddRange(CTREstimationsManager.Instance.GetEstimationsForWBS(wbs.idWBS));
frm = new FrmProjectChooser(items, estimations);
}
else if (copyType.HasFlag(Copy.Estimations))
{
// Single item, with estimations.
WB wbsItem = treeWBS.GetDataRecordByNode(treeWBS.FocusedNode) as WB;
if (wbsItem != null)
frm = new FrmProjectChooser(new List<WB> { wbsItem },
CTREstimationsManager.Instance.GetEstimationsForWBS(wbsItem.idWBS));
}
else if (copyType.HasFlag(Copy.SubItems))
{
// Single item, with sub items
List<WB> items = new List<WB> { (WB)treeWBS.GetDataRecordByNode(treeWBS.FocusedNode) };
items.AddRange(_WBSManager.GetAllChildren(items[0].idWBS));
frm = new FrmProjectChooser(items);
}
else
{
// Single item
WB wbsItem = treeWBS.GetDataRecordByNode(treeWBS.FocusedNode) as WB;
if (wbsItem != null)
frm = new FrmProjectChooser(new List<WB> {wbsItem});
}
}
else if (copyType.HasFlag(Copy.All))
{
if (copyType.HasFlag(Copy.Estimations))
{
// All items, with estimations.
List<WB> wbsItems = _WBSManager.GetWBSForProject(CurrentProject.idProjects);
List<CTREstimation> estimations = new List<CTREstimation>();
foreach (WB wbs in wbsItems)
estimations.AddRange(CTREstimationsManager.Instance.GetEstimationsForWBS(wbs.idWBS));
frm = new FrmProjectChooser(wbsItems, estimations);
}
else
{
// All items
frm = new FrmProjectChooser(_WBSManager.GetWBSForProject(CurrentProject.idProjects));
}
}
if (frm != null)
frm.ShowDialog();
}
[Flags]
private enum Copy
{
None = 0,
Single = 1, // Straight copy of single item
SubItems = 1 << 1, // Include any children in the copy
Estimations = 1 << 2, // Include estimations in the copy
All = 1 << 3 // Copy all items
}
The code will pull "Items" from a TreeList
. "Sub Items" are of the same type as "Items", but Estimations
are a different type. I don't think it's very neat. I've tried googling similar titles to this question but I can't really find anything that helps.
Is there anything I can do to make this neater?