patterncsharpMinor
Manipulating each value in a 2D array
Viewed 0 times
arrayvalueeachmanipulating
Problem
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
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;
}
//****
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;
}
//****
Solution
Removing the check on each iteration is definitely a good idea. My advice: have a separate method for each process. E.G.
then, if you need to select a case, you only need to do it once, and not \$n^2\$ times.
As a general rule, the ONLY things that should go inside an iterative loop are things which could change during the iteration. Anything else should be declared/set beforehand.
SelectedRange = GetSelectedRange()
AllCapsRange(SelectedRange)
Process2Range(SelectedRange)then, if you need to select a case, you only need to do it once, and not \$n^2\$ times.
As a general rule, the ONLY things that should go inside an iterative loop are things which could change during the iteration. Anything else should be declared/set beforehand.
Code Snippets
SelectedRange = GetSelectedRange()
AllCapsRange(SelectedRange)
Process2Range(SelectedRange)Context
StackExchange Code Review Q#101757, answer score: 3
Revisions (0)
No revisions yet.