如何初始化包含结构和简单元素的混合结构对象?

时间:2018-08-01 13:40:10

标签: vb.net visual-studio-2017 initialization object-initializers

使用VB.NET 2017,我声明了一个具有两个成员的结构,其中一个是结构,另一个是字符串,如下所示:

Structure InpFile
    Dim name As String
    Dim reader As StreamReader
    Dim lineIn As String
    Dim lineNum As Integer
End Structure

Structure Opts
    Dim fin As InpFile
    Dim name As String
End Structure

如何在声明时初始化Opts类型的对象?

例如,一种无效的尝试:

Dim obj as Opts = {.fin.name = "filename.txt", .fin.lineNum = 0, .name = "JohnnyMnemonic"}

1 个答案:

答案 0 :(得分:2)

您必须使用With语句。

Dim obj As New Opts With {.fin = New InpFile With {.name = "filename.txt", .lineNum = 0}, .name = "JohnnyMnemonic"}