Please do not get intimidated by the wall of code! My question simply put would be the following - can I somehow reuse the code and place whatever arguments I want in the place of // DoSomething? I do not want to copy and paste the same wall of code, I want to make it less repetitive and easier to maintain. I have wondered about this problem, but I do not see a straightforward solution and I am unable to find one.
for (int i = 0; i < treeListViewDatabase.SelectedObjects.Count; i++)
{
if (treeListViewDatabase.SelectedObjects[i] is NodeValueWithNodes rootNode)
{
for (int m = 0; m < rootNode.ChildTreeViewSets.Count; m++)
{
if (rootNode.ChildTreeViewSets[m] is NodeValueWithNodes childNodeWithNode)
{
for (int k = 0; k < childNodeWithNode.ChildTreeViewSets.Count; k++)
{
if (childNodeWithNode.ChildTreeViewSets[k] is NodeValueWithNodes secondChildNodeWithNode)
{
for (int p = 0; p < secondChildNodeWithNode.ChildTreeViewSets.Count; p++)
{
if (secondChildNodeWithNode.ChildTreeViewSets[p] is NodeValueWithDevices thirdChildNodeWithDevices)
{
for (int r = 0; r < thirdChildNodeWithDevices.ChildDeviceSet.Count; r++)
{
if (dataRows.Contains(thirdChildNodeWithDevices.ChildDeviceSet[r]))
{
dataRows.Remove(thirdChildNodeWithDevices.ChildDeviceSet[r]);
}
// DoSomething()
}
}
}
}
if (childNodeWithNode.ChildTreeViewSets[k] is NodeValueWithDevices secondChildNodeWithDevice)
{
for (int r = 0; r < secondChildNodeWithDevice.ChildDeviceSet.Count; r++)
{
if (dataRows.Contains(secondChildNodeWithDevice.ChildDeviceSet[r]))
{
dataRows.Remove(secondChildNodeWithDevice.ChildDeviceSet[r]);
}
// DoSomething();
}
}
}
}
if (rootNode.ChildTreeViewSets[m] is NodeValueWithDevices childNodeDevices)
{
for (int n = 0; n < childNodeDevices.ChildDeviceSet.Count; n++)
{
if (dataRows.Contains(childNodeDevices.ChildDeviceSet[n]))
{
dataRows.Remove(childNodeDevices.ChildDeviceSet[n]);
}
// DoSomething();
}
}
}
}
if (treeListViewDatabase.SelectedObjects[i] is NodeValueWithDevices rootNodeWithDevices)
{
for (int n = 0; n < rootNodeWithDevices.ChildDeviceSet.Count; n++)
{
if (dataRows.Contains(rootNodeWithDevices.ChildDeviceSet[n]))
{
dataRows.Remove(rootNodeWithDevices.ChildDeviceSet[n]);
}
// DoSomething();
}
}
}
for (int i = 0; i < dataRows.Count; i++)
{
// DoSomething();
}
*EDIT*
Following the advise of the people I have completely changed the code. The arrow-like look to the code was replaced with a simple recursion. This implementation made my issue non-existent as I can loop through the dataRows to perform the necessary action.
private void TraverseTreeView(IList selectedObjects, List<DataRow> dataRows)
{
if (selectedObjects == null)
{
return;
}
foreach (var selectedObject in selectedObjects)
{
if (selectedObject is NodeValueWithNodes withNodes)
{
TraverseTreeView(withNodes.ChildTreeViewSets, dataRows);
}
if (selectedObject is NodeValueWithDevices withDevices)
{
TraverseTreeView(withDevices.ChildDeviceSet, dataRows);
}
if (selectedObject is DataRow dataRow && !dataRows.Contains(dataRow))
{
dataRows.Add(dataRow);
}
}
}