简体   繁体   English

为什么传递变量失败,错误值类型“字符串”无法转换为“字符串()”?

[英]Why does passing variables fails with error value type 'string' cannot be converted into 'string()'?

I'm trying to create an automated test for some C# code using VB.我正在尝试使用 VB 为一些 C# 代码创建一个自动化测试。 Example: For the table Orchestra, i have 1 record for the organisation 1123, whereby user xxx has value for externalId = null and name is Charm White.示例:对于表 Orchestra,我有 1 条组织 1123 的记录,其中用户 xxx 的值为 externalId = null,名称为 Charm White。 So my test would be to check if expected externalId is null and the name is charm white for that user.所以我的测试是检查预期的 externalId 是否为 null 并且该用户的名称是否为魅力白色。

However, when declaring variables of value type string[] externalIds in VB code, it gives error:但是,在 VB 代码中声明值类型 string[] externalIds 的变量时,会报错:

value of type 'String' cannot be converted to 'string()' 'String' 类型的值不能转换为 'string()'

Here's the C# code to be tested:这是要测试的 C# 代码:

  public string[] GetNamesFromExternalIds(string organisationId, string[] externalIds)
    {
        string externalIdsString = null;
        foreach (string externalId in externalIds)
        {
            if (externalIdsString != null)
            {
                externalIdsString += ",";
            }
            externalIdsString += "'" + externalId + "'";
        }

        DbParameterCollection parameters = new DbParameterCollection();
        parameters.Add(new DbParameter("@OrganisationId",SqlDbType.NVarChar, 
        organisationId));

        string sql = string.Format(@"SELECT ExternalId, Name FROM Orchestra 
        WHERE ExternalId IN ({0}) AND OrganisationId = @OrganisationId",
        externalIdsString);

        ListObject list = new ListObject(_Accessor);
        list.Read(sql, parameters);

        List<string> names = new List<string>();
        foreach (string externalId in externalIds)
        {
            foreach (DataRow dataRow in list.Table.Rows)
            {
                if (dataRow["ExternalId"].ToString() == externalId)
                {
                    names.Add(dataRow["Name"].ToString());
                    break;
                }
            }
            names.Add(null);
        }

        return names.ToArray();
    }

Here's what I'm trying into VB:这是我正在尝试进入 VB 的内容:

<TestClass()> Public Class UT_GetNamesFromExternalIds
    <TestMethod()> Public Sub GetNamesFromExternalIds()

        Dim organisationId As String = "1123"

        Dim externalIds() As String ={0}
        Dim user as string = "XXX"
        Dim names As String = String.Empty

        Dim ExpNames As String = "Charm White"
        Dim ExpexternalIds As String = Nothing

        Dim DataServer As New DataServer()
        Dim Accessor = DataServer.GetAccessor()
        Dim _StandardHeader = New StandardHeader
        Dim _AuditProvider = New Audit.AuditProvider(_StandardHeader)
        Dim AD As New Ceridian.Administration.Authentication.AuthenticationData(Accessor, _AuditProvider)

        externalIds = AD.GetNamesFromExternalIds(organisationId, 
         externalIds)

        Assert.AreEqual(1, externalIds.Count)
        Assert.AreEqual(ExpexternalIds, externalIds(0))


        Assert.AreEqual(ExpNames, names)

        Console.WriteLine(ExpNames)
        Console.WriteLine(names)

        Console.WriteLine(ExpexternalIds)
        Console.WriteLine(externalIds)
    End Sub

End Class

Assuming that you are trying to pass 0001214 into the function under test and are expecting Charm White as the result then you need to create a string array with your 0001214 value - pass it to the function and test the result:假设您尝试将0001214传递到正在测试的 function 并期望Charm White作为结果,那么您需要使用0001214值创建一个字符串数组 - 将其传递给 function 并测试结果:

Dim externalIds As String() = {"0001214"}
Dim organisationId As String = "1123"
Dim names = AD.GetNamesFromExternalIds(organisationId, externalIds)

Dim ExpNames As String = "Charm White"

Assert.AreEqual(1, names.Count)
Assert.AreEqual(ExpNames, names(0))

Also, remove the line:另外,删除该行:

Dim names As String = String.Empty

As the value returned from the function under test is a string array not a string.由于被测 function 返回的值是字符串数组而不是字符串。 I have also moved your declaration of organisationId so that it is close to the code that uses it.我还移动了您对organisationId的声明,以便它接近使用它的代码。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么字符串不能转换为日期时间? - Why string cannot be converted to dateTime? 为什么装箱的整数值会隐式转换为字符串类型? - Why can boxed integer value get implicitly converted to string type? 绑定无法转换为 System.String 类型 - Binding cannot be converted to type System.String 在LINQ dat source / gridview中使用文本框值作为参数:“String”类型的值无法转换为“Double”类型 - Using textbox value as parameter in LINQ dat source/gridview : A value of type 'String' cannot be converted to type 'Double' 数据源中String类型的给定值无法转换为指定目标列的float类型 - The given value of type String from the data source cannot be converted to type float of the specified target column 来自数据源的 String 类型的给定值无法转换为指定目标列的 nvarchar 类型 - The given value of type String from the data source cannot be converted to type nvarchar of the specified target column 来自数据源的String类型的给定值不能转换为指定目标列的varbinary类型 - The given value of type String from the data source cannot be converted to type varbinary of the specified target column 数据源中String类型的给定值无法转换为指定目标列的int类型 - The given value of type String from the data source cannot be converted to type int of the specified target column SqlBulkCopy - 来自数据源的 String 类型的给定值无法转换为指定目标列的货币类型 - SqlBulkCopy - The given value of type String from the data source cannot be converted to type money of the specified target column 来自数据源的 String 类型的给定值无法转换为指定目标列的 int 类型。 - The given value of type String from the data source cannot be converted to type int of the specified target column.'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM