C# modifying string array from one scope level dow

2019-07-17 08:05发布

问题:

I am a novice at C#, yet I know I should be able to figure this out. My searching skills have not given me a direct answer either.

I have two application settings that are stored in string arrays (they have been split from a , separated list).

Ultimately I want to run one chunk of code, conditional upon both settings.

Conditions are:

  1. If settings exist in array 1 (domattributes), run the code on each setting value.
  2. If settings also exist in array 2 (intlattributes), run the code on each setting value contained in either array 1 or array
  3. Below is how I have tried to do it using an if/else statement to build out the string array, but it doesn't work.

I get the error

The name 'attributeIds' does not exist in the current context

I am assuming it is because the string array is actually built in the if/else statement, and is probably in a different scope from the foreach method that is trying to use it. Any help would be appreciated. Here's the code:

if (!string.IsNullOrEmpty(DomAttributesSetting))
{
    if (!string.IsNullOrEmpty(IntlAttributesSetting))
    {
        string[] domattributeIds = DomAttributesSetting.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
        string[] intlattributeIds = IntlAttributesSetting.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
        string[] attributeIds = new string[domattributeIds.Length + intlattributeIds.Length];
        Array.Copy(domattributeIds, attributeIds, domattributeIds.Length);
        Array.Copy(intlattributeIds, 0, attributeIds, domattributeIds.Length, intlattributeIds.Length);
    }
    else
    {
        string[] attributeIds = DomAttributesSetting.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
    }
    foreach (string attributeId in attributeIds)
    {
        PersonAttribute personAttribute = (PersonAttribute)person.Attributes.FindByID(int.Parse(attributeId));
        if (personAttribute == null)
        {
            personAttribute = new PersonAttribute(person.PersonID, int.Parse(attributeId));
        }...

回答1:

You need to declare attributeIds only once, and it must be declared outside the if statement so that it is visible to the rest of the method.

Try this:

string[] attributeIds;
if (!string.IsNullOrEmpty(IntlAttributesSetting))
{
    string[] domattributeIds = DomAttributesSetting.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
    string[] intlattributeIds = IntlAttributesSetting.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
    attributeIds = new string[domattributeIds.Length + intlattributeIds.Length];
    Array.Copy(domattributeIds, attributeIds, domattributeIds.Length);
    Array.Copy(intlattributeIds, 0, attributeIds, domattributeIds.Length, intlattributeIds.Length);
}
else
{
    attributeIds = DomAttributesSetting.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
}

foreach (string attributeId in attributeIds)
{
    // etc...
}


回答2:

if attributeIds isn't in your scope, you can just put it in your scope. Before your if/else, put this

string[] attributeIds = null;

then, you can access it in the foreach loop, make sure you just assign to it and don't try to create it again

attributeIds = new string[domattributeIds.Length + intlattributeIds.Length];


回答3:

Whenever you declare a variable within a pair of braces ({}), it is in that scope - it is not known outside of it.

This means that your domattributeIds, intlattributeIds and attributeIds variables are only known within the if statement surrounding them (and you are creating a second attributeIds variable within the else scope). You need to declare them outside of that scope in order to use them outside these scopes:

string[] attributeIds;

if (!string.IsNullOrEmpty(IntlAttributesSetting))
{
    ...
    attributeIds = new string[domattributeIds.Length + intlattributeIds.Length];
    ...
    Array.Copy(intlattributeIds, 0, attributeIds, domattributeIds.Length, intlattributeIds.Length);
}
else
{
    attributeIds = DomAttributesSetting.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
}


回答4:

You are declaring attributeIds inside the if statement (and again in the else statement). In order to access that variable outside of the if/else scope, declare it in the parent scope:

string[] attributeIds = null;
if (condition1) {
    attributeIds = ...
} else {
    attributeIds = ...
}

foreach (string attributeId in attributeIds) {
    ....
}