2

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
  • thanks for the Edit, Obed. You beat me to it :) Commented May 8, 2012 at 21:07

4 Answers 4

6

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...
}
Sign up to request clarification or add additional context in comments.

1 Comment

Oh my gosh, okay, where is the newb sign? I'll wear it in shame :( I did not realize you could declare a string array with no initialization, then simply assign values to it. Thank you Mark, and congrats on free points for being the quick-draw ;)
0

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];

Comments

0

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);
}

Comments

0

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) {
    ....
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.