使用VB.net引用类

时间:2018-08-02 15:13:45

标签: vb.net initialization

我已经调用了clientdetails,我希望将其整体返回到JSONConvert方法以序列化JSON。

我创建了一个具有我所需的属性类型(TextA,TextB)等的类。

我可以同时引用TransactionCount和TransactionType,因为它们是ClientDetails的一部分,但是当我尝试引用Transaction的TextA时,它指出TextA不是ClientDetails的成员-这我知道这就是为什么我明确声明.Transactions的原因。 .TextA。

如果我在新变量下声明了独立的事务,则可以引用它们,但是我需要将它们全部在ClientDetails下声明,以传递给序列化器。

有人可以指出我在这里做错了什么吗?还在学习。

Public Class JSON

Public Shared Function SerializeObject()

    Dim clientdetails As New ClientDetails() With {.TransactionCount = "1", .TransactionType = "Q", .Transactions.TextA} 'Unable to Refer to any Property of Transactions.

    'Dim Trans As New Transactions()

    'Trans.TextA= "Test"

    Dim output As String = JsonConvert.SerializeObject(clientdetails, Newtonsoft.Json.Formatting.Indented)

    Return output
End Function

End Class

Public Class ClientDetails
Public Property Transactions As New Transactions()
Public Property [TransactionType] As String
Public Property [TransactionCount] As Integer
End Class

Public Class Transactions

Public Property [RecordID] As String
Public Property No As String
Public Property TextA As String
Public Property TextB As String
Public Property Initial As String
Public Property Flag As String
Public Property Sex As String
Public Property Area As String
Public Property Type As String
Public Property IDNO As String

End Class

1 个答案:

答案 0 :(得分:1)

您可以使用以下语法:

Dim clientdetails As New ClientDetails() With {.TransactionCount = "1", .TransactionType = "Q", .Transactions = New Transactions() With {.TextA = "Test"}}

或更易读的代码:

Dim trans As New Transactions
trans.TextA = "Test"

Dim clientDetails As New ClientDetails()

With clientDetails
    .TransactionCount = "1"
    .TransactionType = "Q"
    .Transactions = trans
End With