首页 > 解决方案 > 如何使用 vb.net 创建具有不同类型数据的列表?

问题描述

我需要创建一个具有不同数据类型元素的列表,例如:

{{10, 10}, "IT", 1, "Test", {100, 100}, "Test"} 

分别:

{object, string, integer, string, object, string}

我尝试将其声明为对象列表或使用Tuple(Of Object, String, Integer, String, Object, String) ,但是当我给它们值时,

“数组初始值设定项的维度太少”

发生错误。

声明变量的类:

Public Class SignatureResponse

    Public signature As Tuple(Of Object, String, Integer, String, Object, String)

    Sub New()
        Me.signature = Nothing

    End Sub

    Sub New(ByVal signature As Tuple(Of Object, String, Integer, String, Object, String))
        Me.signature = signature
    End Sub
End Class

我使用参数并为其分配值的类:

Public Class Signature
      Inherits System.Web.Services.WebService

    <WebMethod()>
    <ScriptMethod(ResponseFormat:=ResponseFormat.Json, UseHttpGet:=False, XmlSerializeString:=False)>
    Public Function SendIdDocuments(ByVal idDocument As String, ByVal content As String, ByVal userId As String)
       Dim respDocs As New SignatureResponse
                respDocs.signature = {{10, 10}, "IT", 1, "Testing", {100, 100}, "Test Signature"}
             'Other part of development
            JSONString = JsonConvert.SerializeObject(respDocs)

        Return JSONString
    End Function

End Class

我只发送代码中更重要的部分,它们是使用的更多参数,可以完美运行,除了这个。

错误: 错误

请问有什么帮助吗?

标签: asp.netvb.netlistarraylisttuples

解决方案


如果您使用上面的旧元组语法,那么您无法按照您显示的方式创建新语法。你所要做的:

    respDocs.signature =
        New Tuple(Of Object, String, Integer, String, Object, String)({10, 10}, "IT", 1, "Testing", {100, 100}, "Test Signature")

推荐阅读