As the title states. I need a different type of icon for the different nodes types. at the moment I have this hacky way of achieving that.

for (var x = 0; x <= nodeList.Count - 1; x++)
{
if (xNode.Attributes["TYPE"].Value == "server.point.AV")
{
var tNode2 = new TreeNode(xNode.Attributes["NAME"].Value, AnalogImagesIndex, AnalogImagesIndex);
tNode2 = inTreeNode.Nodes[x];
}
else if (xNode.Attributes["TYPE"].Value == "schedule.NSPDigitalSchedule")
{
var tNode2 = new TreeNode(xNode.Attributes["NAME"].Value, ScheduleImageIndex, ScheduleImageIndex);
tNode2 = inTreeNode.Nodes[x];
}
else if (xNode.Attributes["TYPE"].Value == "server.point.SV")
{
var tNode2 = new TreeNode(xNode.Attributes["NAME"].Value, StringImageIndex, StringImageIndex);
tNode2 = inTreeNode.Nodes[x];
}
else if (xNode.Attributes["TYPE"].Value.StartsWith("trend"))
{
var tNode2 = new TreeNode(xNode.Attributes["NAME"].Value, TrendImageIndex, TrendImageIndex);
tNode2 = inTreeNode.Nodes[x];
}
.
.
.and so on


I do not want to add the long list of else if's.

• Do you have 1 image type for each type or you have several groups and each group has 1 image? – Ziv Weissman Sep 5 '16 at 13:13
• Just one image list – QANew Sep 5 '16 at 23:30

The moment you start copy-pasting code and only changing one small thing, it's time to move that code to a method.

private void CreateAndAddNode(WhateverTypeImagesIndexIs imagesIndex, int x)
{
var tNode2 = new TreeNode(xNode.Attributes["NAME"].Value, imagesIndex, imagesIndex);
tNode2 = inTreeNode.Nodes[x];
}


I'm assuming AnalogImagesIndex etc. all inherit from some base type; that's the one to use as the method's parameter.

Moreover, all of those ifs can be changed to a switch.

• all of those ifs can be changed to a switch. - nope, they cannot, see: .Value.StartsWith("trend") – t3chb0t Sep 5 '16 at 8:36
• Not until C# 7 where you can have conditions in case – t3chb0t Sep 5 '16 at 8:36
• @t3chb0t Oops, didn't notice the StartsWith. – BCdotWEB Sep 5 '16 at 8:51
• I have changed few into swithch switch (xNode.Attributes["TYPE"].Value) { case "server.point.BV": { Then back to default: if where I need to check the starting string – QANew Sep 5 '16 at 23:31
• I want to be like you when I grow up ^.^ – Tolani Sep 6 '16 at 8:35

It doesn't look like you would need the for loop. You can do the same much simpler with a foreach and a little bit of linq:

// assuming they are ints
var AnalogImagesIndex = 0;
var TrendImageIndex = 2;

// simplifies the ifs
var imageIndicies = new Func<string, int?>[]
{
attr => attr == "server.point.AV" ? AnalogImagesIndex : (int?)null,
attr => attr.StartsWith("trend") ? TrendImageIndex : (int?)null,
// ...
};

var imageIndex = imageIndicies
.Select(x => x(xNode.Attributes["NAME"].Value))
.First(x => x.HasValue)
.Value;


To do the rest you can use the helper from BCdotWEB's answer.

Another assumption is that there is no else at the end, otherwise you need to use FirstOrDefault or provide an else lambda.

or with even less repetition the extreme solution:

var getImageIndexOrDefault = new Func<bool, int, int?>(
(condition, value) => condition ? value : (int?)null);

var imageIndicies = new Func<string, int?>[]
{
attr => getImageIndexOrDefault(attr == "server.point.AV", AnalogImagesIndex),
attr => getImageIndexOrDefault(attr.StartsWith("trend"), TrendImageIndex),
};

• I have multiple else at the end – QANew Sep 5 '16 at 23:34
• Wondering if i could skip the node and continue when it comes across a node not satisfying any of the conditions – QANew Sep 6 '16 at 0:01
• I want to be like you when I grow up ^.^ – Tolani Sep 6 '16 at 8:34

On top of @BCdotWEB & @t3chb0t answers you can use the .Any() condition.

For example:

List<string> group1 = new List<string> {"schedule.NSPDigitalSchedule, schedule.Whatever, WhenEver.WhatEver"};

List<string> group2 = ...

//in foreach loop:
string nodeValue = xNode.Attributes["TYPE"].Value;
if (group1.Any(m=>m==nodeValue))
{
//Call the method for adding image for group1
continue; //This will go to the next iteration so you don't need nested if's
}

if (group2.Any(m=>nodeValue.StartWith(m))
{
//Call the method for adding image for group2
continue;
}

//and so on.


## Edit:

Based on the comment you could build your groups for the index you need them like so:

List<string> group1 = new List<string> {"schedule.ImageIndex1, schedule.ImageIndex2, schedule.ImageIndex3"};
//group2...

List<List<string>> groups = new List<List<string>>();
groups.Add(group1, group2...); //or just build in constuctor

foreach (var xNode in xNodes)
{
string nodeValue = xNode.Attributes["TYPE"].Value;
//another foreach on all the groups:
foreach (var group in groups)
{
int currentIndex = group.IndexOf(nodeValue);
if (currentIndex > -1)
{

• There is one problem with .Any - the OP needs a different image index for each attribute value and not just a check if it has any of those values. – t3chb0t Sep 6 '16 at 8:40
• AnalogImagesIndex = 3; //AnalogImagesIndex {get, set}... etc – QANew Sep 7 '16 at 23:05