This code is about manipulating each value in a 2D array. My method below is called based on how I want the values in the array to be manipulated (process).
For the first method in the switch statement - as an example, I am calling AllCaps()
method to manipulate the value.
What I am trying to avoid is repeating statements when I can put it in a method instead and just call it when needed. With my code below, though, it seems like I am making the program work too hard by performing a check on each iteration - which could be in the thousands.
Is it effective to call a method for processing each 2D array value inside the nested for loop? Or call the method first and perform the nested for loop inside each method?
private void ProcessSelectedRange(string process)
{
selectedRange = GetSelectedRange();
// initialize and populate 2d array with values from selected range
curValue = new object[,] { };
curValue = selectedRange.Value;
// possibly more statements
// loop through curValue 2d array
for (int i = 1; i <= curValue.GetLength(0); i++)
{
for (int j = 1; j <= curValue.GetLength(1); j++)
{
string str = string.Empty;
// continue if curValue[i, j] is null or empty
try
{
str = curValue[i, j].ToString();
if (string.IsNullOrEmpty(str)) continue;
}
catch { continue; }
//********************************************************************
// call method depending on what we want to do with the value
switch (process)
{
case "AllCaps": AllCaps(str); break;
case "Process2": Process2(str); break;
case "Process3": Process3(str); break;
case "Process4": Process4(str); break;
// possibly more methods
default: Process5(str); break;
}
//********************************************************************
}
}
}