I’m writing a script to check a bunch of Excel spreadsheets for certain values, and I’ve wound up with code that looks like this:
public bool checkContents(Excel._Worksheet sht, string address, string cellValue)
{
Excel.Range tempRange = sht.get_Range(address);
return Convert.ToString(tempRange.Value) == cellValue;
}
public string getVersion(Excel._Worksheet sht)
{
if (checkContents(sht,"a4","Changes for Version 24"))
{
return "24";
}
else if (checkContents(sht,"a1","Changes for Version 23 (Official)"))
{
return "23";
}
else if (checkContents(sht,"a2","Changes for Version 22"))
{
return "22";
}
//and so on for another 10 if-else blocks
}
I know that for a given sheet, only one of the if statements will be true.
Is there a more concise way to write this function other than as a long sequence of ifs?
You could try something like this (untested, so may have some syntactical errors)