English 中文(简体)
C# 从一个范围下调的阵列
原标题:C# modifying string array from one scope level down

我是C#的一座新陈词,但我知道我应该能够说明这一点。 我的搜索技能没有给我一个直接答案。

我有两个应用环境储存在阵列中(这些地方已从一个分离的名单中分离)。

最后,我要以两种情况为条件,管理一个法典的空白。

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.

我发现错误。

姓名属性 在当前情况下不存在。

我假定,这是因为插座阵列实际上是在假设/声明中建造的,而且可能与试图使用它的每一方法不同。 希望得到任何帮助。 该法典:

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));
        }...
最佳回答

您必须宣布<条码>,仅限<>>>>>oce <>/strong>,而且必须在<条码>上公布,以便该表能见到其他方法。

引证:

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...
}
问题回答

如果属性, 你的直言不.,你只能把它放在你们的范围之内。 在您提出之前,

string[] attributeIds = null;

那么,你可以坐在旁边,确保你被指派到这里,不试图再造。

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

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

这意味着:domattributeIds ,intlattributeIdsattributeIds的变量只有within。 你们需要宣布它们不属于这一范畴,以便在这些范围之外使用:

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

阁下 如果发言(另在发言中再次发言)就座。 为了在范围之外获取这一变量,在母体范围内宣布:

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

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




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签