如何初始化包含结构和简单元素混合的结构对象?
How to initialize a structure object containing a mix of structure and simple elements?
使用VB.NET2017,我声明了一个结构体,它有两个成员,一个是结构体,一个是字符串,如下:
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"}
您必须使用 With 语句。
Dim obj As New Opts With {.fin = New InpFile With {.name = "filename.txt", .lineNum = 0}, .name = "JohnnyMnemonic"}
使用VB.NET2017,我声明了一个结构体,它有两个成员,一个是结构体,一个是字符串,如下:
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"}
您必须使用 With 语句。
Dim obj As New Opts With {.fin = New InpFile With {.name = "filename.txt", .lineNum = 0}, .name = "JohnnyMnemonic"}