从一个作用域级下调C#修改字符串数组 [英] C# modifying string array from one scope level down

查看:96
本文介绍了从一个作用域级下调C#修改字符串数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在C#新手,但我知道我应该能想出解决办法。我的搜索技巧没有任何给我一个直接的答案。

我有存储在字符串数组(它们已经从分割,分隔的列表)两个应用设置。

最后,我要运行code的一大块​​,在两种设置条件。

的条件是:


  1. 如果阵列中存在设置1(domattributes),在各设定值code。

  2. 如果设置也是在阵列2(intlattributes)存在,运行无论是包含数组中的每个设定值code 1或阵列

  3. 下面就是我试图做它用if / else语句打造出来的字符串数组,但它不工作。

我得到的错误


  

名称'attributeIds'不存在于当前上下文存在


我假设这是因为字符串数组实际上是建立在if / else语句,并可能是从的foreach方法试图使用一个不同的范围。任何帮助将是AP preciated。这里的code:

 如果(!string.IsNullOrEmpty(DomAttributesSetting))
{
    如果(!string.IsNullOrEmpty(IntlAttributesSetting))
    {
        字符串[] = domattributeIds DomAttributesSetting.Split(新的char [] {','},StringSplitOptions.RemoveEmptyEntries);
        字符串[] = intlattributeIds IntlAttributesSetting.Split(新的char [] {','},StringSplitOptions.RemoveEmptyEntries);
        字符串[] = attributeIds新的字符串[domattributeIds.Length + intlattributeIds.Length];
        Array.Copy(domattributeIds,attributeIds,domattributeIds.Length);
        Array.Copy(intlattributeIds,0,attributeIds,domattributeIds.Length,intlattributeIds.Length);
    }
    其他
    {
        字符串[] = attributeIds DomAttributesSetting.Split(新的char [] {','},StringSplitOptions.RemoveEmptyEntries);
    }
    的foreach(在attributeIds串attributeId)
    {
        PersonAttribute personAttribute =(PersonAttribute)person.Attributes.FindByID(int.Parse(attributeId));
        如果(personAttribute == NULL)
        {
            personAttribute =新PersonAttribute(PERSON.PERSONID,int.Parse(attributeId));
        } ...


解决方案

您需要声明 attributeIds 唯一的一次,然后必须将其声明在如果语句外,这样它是对方法的其余部分是可见的。

试试这个:

 的String [] attributeIds;
如果(!string.IsNullOrEmpty(IntlAttributesSetting))
{
    字符串[] = domattributeIds DomAttributesSetting.Split(新的char [] {','},StringSplitOptions.RemoveEmptyEntries);
    字符串[] = intlattributeIds IntlAttributesSetting.Split(新的char [] {','},StringSplitOptions.RemoveEmptyEntries);
    attributeIds =新的字符串[domattributeIds.Length + intlattributeIds.Length];
    Array.Copy(domattributeIds,attributeIds,domattributeIds.Length);
    Array.Copy(intlattributeIds,0,attributeIds,domattributeIds.Length,intlattributeIds.Length);
}
其他
{
    attributeIds = DomAttributesSetting.Split(新的char [] {','},StringSplitOptions.RemoveEmptyEntries);
}的foreach(在attributeIds串attributeId)
{
    //等等...
}

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

解决方案

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

这篇关于从一个作用域级下调C#修改字符串数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆