非可调用成员'String.Length'不能像方法一样使用

时间:2013-07-09 16:09:03

标签: c#

我正在尝试使用脚本组件来处理我正在处理的HRIS包。 我收到一个非Invocable错误。我不熟悉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




    }



}

}  

7 个答案:

答案 0 :(得分:10)

Length()不是方法,而是属性。只需在没有括号的情况下使用Length

答案 1 :(得分:4)

编译错误对我来说非常清楚。这样:

int length_full = fullName.Length();

应该是:

int length_full = fullName.Length;

......同样适合你的其他方面。这是因为String.Length是属性,而不是方法。

除了修复你的代码之外,你应该再次查看编译错误,并尝试弄清楚为什么你会发现它令人困惑 - 这可能会帮助你找到你应该更熟悉的C#。我强烈建议你在之前更熟悉C#的基础知识,你会对数据库等其他东西造成太多麻烦 - 这样可以让以后更容易诊断出任何问题。

答案 2 :(得分:3)

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

int length_full = fullName.Length;

答案 3 :(得分:3)

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

答案 4 :(得分:2)

长度字符串类型的属性,因此请删除该paranthesis。 请致电:

suffix.Length;

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

因为你必须在循环中将i定义为int。

另外,

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

使用:

lastName = entire_name[i].ToString();

答案 5 :(得分:0)

for循环问题结束时的“i”应该可以通过用

替换循环声明来修复
for(int i = compare_length; i < length_full - 1; i++)

答案 6 :(得分:0)

仅使用下面显示的长度而不是长度()不是方法。

fullName.Length;