使用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"}
答案 0 :(得分:2)
您必须使用With语句。
Dim obj As New Opts With {.fin = New InpFile With {.name = "filename.txt", .lineNum = 0}, .name = "JohnnyMnemonic"}