【问题标题】:Non-Invocable Member 'String.Length' cannot be used like a method不可调用成员“String.Length”不能像方法一样使用
【发布时间】:2013-07-07 08:18:05
【问题描述】:

我正在尝试让一个脚本组件为我正在处理的 HRIS 包工作。 我收到一个不可调用的错误。我不熟悉 C#,所以我不知道如何纠正这个问题。我正在与一个油炸食品合作,该油炸食品改变了另一个 STO 成员之前给我的代码。

.Length();是导致冲突的原因,“for”语句末尾的“i”也是如此。

修改后的代码如下。

/* Microsoft SQL Server Integration Services Script Component
*  Write scripts using Microsoft Visual C# 2008.
*  ScriptMain is the entry point class of the script.*/

using System;
using System.Data;
using Microsoft.SqlServer.Dts.Pipeline.Wrapper;
using Microsoft.SqlServer.Dts.Runtime.Wrapper;
using System.Text.RegularExpressions;

[Microsoft.SqlServer.Dts.Pipeline.SSISScriptComponentEntryPointAttribute]
public class ScriptMain : UserComponent
{

public override void PreExecute()
{
    base.PreExecute();
    /*
      Add your code here for preprocessing or remove if not needed
    */
}

public override void PostExecute()
{
    base.PostExecute();
    /*
      Add your code here for postprocessing or remove if not needed
      You can set read/write variables here, for example:
      Variables.MyIntVar = 100
    */
}

public override void Input0_ProcessInputRow(Input0Buffer Row)
{
    const string pFirstName = @"^[A-Z]([-']?[a-z]+)*";
    const string pSuffix = @"((Jr|Sr|I|V|X)( ?))+";
    const string pInitial = @"(?<=\s)[A-Z](?=\s)";
    const string pLastName = @"(?!(?:Jr|Sr|I|V|X|^))([A-Z][-'\s]?[a-z]+)";

    string fullName = Row.Name.ToString();
    string firstName = Regex.Match(fullName, pFirstName).Value;
    string suffix = Regex.Match(fullName, pSuffix).Value;
    string initial = Regex.Match(fullName, pInitial).Value;
    string lastName = Regex.Match(fullName, pLastName).Value;

    /*
     * 20130708 - Edited by Armando Allison
     * 
     * 
     * 1. Get the length of the entire full name
     * 2. Get the firstName length value
     * 3. Get the initial length value
     * 4. Get the suffix length value
     * 5. Get the lastName length value
     * 
     * 
     */


    int length_full = fullName.Length();     //length of fullName
    int length_first = firstName.Length();    //length of the firstName
    int length_suffix = suffix.Length();   //length of the suffix
    int length_initial = initial.Length();  // length of the initial
    int length_lastName = lastName.Length(); //length of the lastName

    int compare_length = 0;
    compare_length = length_full - (length_first - length_initial - length_suffix); //      if it pulled the data correctly, lastName length should equal compare_length


    if (length_lastName == compare_length)
    {
        if (!string.IsNullOrEmpty(initial))
            lastName += " " + initial;

        if (!string.IsNullOrEmpty(suffix))
            lastName += " " + suffix;

        Row.FirstName = firstName;
        Row.LastName = lastName;

    }
    else
    {
        // if the lastName doesnt match the compare_length
        // you will have to do some more editing.

        // 1. put entire full name into a string array
        // 2. remove all the suffix, initial, and firstName
        // 3. then output the entire lastName in a for loop with the remaining full name array 
        //    remove to remove the other parts of the array that is not needed
        // Pseudo code

        char[] entire_name;
        entire_name = new char[length_full];
        entire_name = fullName.ToCharArray(0, length_full);
        for (i = compare_length; i < length_full - 1; i++)
        {
            lastName = (String)entire_name[i];
        }
        // loop entire array to include the entire full name
        // next remove the parts of the array that is not needed
        // then output just the last name with another for loop




    }



}

}  

【问题讨论】:

    标签: c#


    【解决方案1】:

    Lengthstring 类型的属性,因此请删除括号。 只需致电:

    suffix.Length;
    

    还有

    for(int i = compare_length; i < length_full - 1; i++)
    {
         //Do something
    }
    

    因为您必须在循环内将 i 定义为 int。

    另外,

    lastName = (String)entire_name[i]; 错了

    使用:

    lastName = entire_name[i].ToString();
    

    【讨论】:

    • 这看起来修复了错误。感谢您提供所有信息。我将与帮助我写这篇文章的朋友分享。
    • 修复错误后,我运行了包。它创建了 FirstName 和 LastName 列,但没有插入任何数据。这两列的所有行都是空白的。有什么想法吗?
    • 有趣的是,接受的答案比所有其他答案的得票少,对于像我这样拥有 256 名代表的人来说根本不公平:D
    • 我喜欢你的回答,因为它很详细,它回答了我的两个问题。我实际上喜欢几个答案,但我知道我只能选择 1。我是这个网站的新手,前几天才知道投票和答案接受。希望我能及时提高我的代表,并像你们其他人一样知识渊博。再次感谢您的回答。 ;)
    • 此问题已修复。然而,另一个出现了。如果您有见解,请访问stackoverflow.com/questions/17556005/… 希望您不是给我-1 的人,哈哈。
    【解决方案2】:

    编译错误对我来说似乎很清楚。这个:

    int length_full = fullName.Length();
    

    应该是:

    int length_full = fullName.Length;
    

    ... 你的其他行也是如此。这是因为String.Length 是一个属性,而不是一个方法。

    除了修复代码之外,您还应该再次查看编译错误并尝试找出您发现它令人困惑的原因 - 这可能会帮助您找出您应该更熟悉的 C# 部分。我强烈建议您先熟悉 C# 的基础知识,您过多地使用数据库等其他东西之前 - 这将使以后诊断任何问题变得容易得多。 p>

    【讨论】:

    • 感谢您的建议。我希望这很容易。我学习了一些 SQL 类,但没有使用 C#。有点被推上了这个角色。
    • @BrianD.Brubaker:这并不意味着您不能暂停其余的工作,学习 C# 的基础知识,然后再回到它...
    【解决方案3】:

    Length is a property,不是方法,可以省略()

    int length_full = fullName.Length;
    

    【讨论】:

      【解决方案4】:

      只使用下面显示的 Length 而不是 Length() 它不是方法。

      fullName.Length;
      

      【讨论】:

        【解决方案5】:

        for 循环问题末尾的“i”应该可以通过将循环声明替换为来修复

        for(int i = compare_length; i < length_full - 1; i++)
        

        【讨论】:

          【解决方案6】:

          Length 是一个属性,而不是一个函数。只需删除括号即可。

          【讨论】:

            【解决方案7】:

            Length() 不是方法,而是属性。只需使用不带括号的Length

            【讨论】:

            • 效果很好,感谢您的意见。我只是在深入研究 SQL 的功能。这是我第一次使用 C#。最后的“i”呢?知道为什么它告诉我“当前上下文中不存在名称'i'”。
            • 使用前需要声明i,例如: for(int i = 0; i &lt; ...;i++)
            猜你喜欢
            • 2013-08-10
            • 1970-01-01
            • 1970-01-01
            • 2017-04-11
            • 2016-05-01
            • 2016-04-11
            • 2018-05-31
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多