Colton Skees Colton Skees - 1 year ago 125
C# Question

Reassign a variable a value in a foreach/for loop, and then use that value outside of the loop?

I want to use and display the value of c(which I assigned a variable named test2 to) used in the foreach loop, but the variable shows null value when used outside of the foreach loop. How can I access the value of test2 or c? I tried to append the value of c/test2 to a list at one point, but could not manage to display the value of that list as a string.

class encryptionFunctionClass
{

List<string> textConvertedToArray = new List<string>();
List<string> charToMessageBox = new List<string>();
string inputText;
string inputTextToChar;
char test1; // Poor Naming Conventions Ik.
char test2; // Poor Naming Conventions Ik.
bool isReturn = false;
bool isNotReturn = true;
int test3;
char c;

public int caesarEncryptText(string inputText, int encryptionNumberShift)
{
textConvertedToArray.Add(inputText);
foreach (char c in inputText.ToString().ToCharArray())
{
int i = 0;
char test2 = c;
test1 = test2;
if ((char)c + encryptionNumberShift >= 24)
{
isReturn = true;

test2 = c;
charToMessageBox.Add(c.ToString());

}
else
{
isNotReturn = true;
test2 = c;
charToMessageBox.Add(c.ToString());
}

}
while(true)

MessageBox.Show(test2.ToString());

if(isReturn == true)
{
return (char)c - (int)26;
}
else
{
if(isNotReturn == true)
{

return (char) + (int)24;
}
}
}

}

RBT RBT
Answer Source

C# follows block scoping. Same named variable c and test2 are also present as member variables of your class. Using the same named variables inside your method creates new variable altogether and your member variables declared at class level get suppressed. So any change made to c and test2variables inside caesarEncryptText method will have no impact on member variables.

class encryptionFunctionClass
{
    char test2; // Poor Naming Conventions Ik.
    char c;

    public int caesarEncryptText(string inputText, int encryptionNumberShift)
    {
        textConvertedToArray.Add(inputText);
        foreach (char c in inputText.ToString().ToCharArray())
        {
            //other code
            //this test2 variable is NOT your member variable
            char test2 = c;
            //remaining code
        }
     }
}

To actually change the member variables instead you have two ways:

  1. Do not declare test2 variable locally inside your method. Then if you change test2 inside your method, it will change your member variable. This is what you should ideally be doing. Having same named variables at class level and at method level is dirty code.
  2. Access the member variable using this keyword inside your method if you want to change the member variable instead as shown below:

    foreach (char c in inputText.ToString().ToCharArray())
    {
        //other code
        //this test2 variable is NOT your member variable
    
        this.test2 = c;
    
        //remaining code
    }
    
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download