0

How can i declare and set a custom type in one line.

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

In this mini example the WorkingExample shows the behavior i want and the NotCompiling part shows what i want to write but i am not able to.

Please show how something like this Dim example As Stackoverflow = {.stack = "stack", .overflow = "overflow"} can be written as one line.

Johannes
  • 6,490
  • 10
  • 59
  • 108
  • 1
    [tag:VBA] is not like [tag:VB.NET]. You cannot declare and set variable in one line. The closest you could get is what's discussed in here: http://stackoverflow.com/questions/3256122/can-i-simultaneously-declare-and-assign-a-variable-in-vba – L42 Jul 25 '14 at 09:44

1 Answers1

1

The colon is a line ending like a carriage return.

Sub WorkingExample():Dim example As Stackoverflow:example.stack = "stack":example.overflow = "overflow":MsgBox (example.stack + example.overflow):End Sub
D.Ddgg
  • 91
  • 2
  • Be a little careful with these. I've had occasional unexpected results when trying to use the `:` to end a line. – CodeJockey Jul 25 '14 at 13:35