如何在一行中声明和定义自定义类型的变量

时间:2014-07-25 09:16:16

标签: vba variables types declaration definition

如何在一行中声明和设置自定义类型。

Option Explicit

Type Stackoverflow
    stack As String
    overflow As String
End Type

Sub WorkingExample()
    Dim example As Stackoverflow
    example.stack = "stack"
    example.overflow = "overflow"
    MsgBox (example.stack + example.overflow)
End Sub

Sub NotCompiling()
    Dim example As Stackoverflow = {.stack = "stack", .overflow = "overflow"}
    MsgBox (example.stack + example.overflow)
End Sub

在这个迷你示例中,WorkingExample显示了我想要的行为,而NotCompiling部分显示了我想写的内容,但我无法做到。

请说明如何将Dim example As Stackoverflow = {.stack = "stack", .overflow = "overflow"}这样的内容写成一行。

1 个答案:

答案 0 :(得分:1)

冒号是一个像回车一样结束的行。

Sub WorkingExample():Dim example As Stackoverflow:example.stack = "stack":example.overflow = "overflow":MsgBox (example.stack + example.overflow):End Sub