Tomislav Horvat
Tomislav Horvat

Reputation: 163

How to declare type that isn't variable

How to declare type whose declared children I can call by name?

Some thing like this:

delphi:

TSuit = (Hearts, Diamonds, Clubs, Spades)

I want to declare list of items, they don't need to contain value, which I can call while writing code to make code more readable and easier to modify.

I tried this but I can't figure out how to do it:

declaration:

Public Enum EType
    square = 0
    triangle = 1
    circle = 2
End Enum

Public Sub LOK(line As Byte, SlType As EType)
    MsgBox ("test")
End Sub

calling:

LOK(1, square)

Is this even possible in VBA?

Upvotes: 3

Views: 69

Answers (2)

Pᴇʜ
Pᴇʜ

Reputation: 57683

circle seems to be a reserved key word and cannot be used. If you change it to something else like eCircle everthing works as intended.

Option Explicit

Public Enum EType
    eSquare = 0
    eTriangle = 1
    eCircle = 2
End Enum

Public Sub LOK(line As Byte, SlType As EType)
    MsgBox "test: " & SlType
End Sub

Sub test()
    LOK 1, eSquare
    LOK 1, eTriangle
    LOK 1, eCircle
End Sub

Upvotes: 4

Vityata
Vityata

Reputation: 43585

Run TestMe and the Messagebox should appear:

Public Enum EType
    square = 1
    triangle = 2
    kolelo = 3
End Enum

Public Sub LOK(line As Byte, SlType As EType)
    MsgBox ("test")
End Sub

Sub TestMe()
    LOK 1, square
End Sub

However, if you are trying to actually get the word behind the enumeration, e.g. print "square" or "triangle" or "kolelo" (circle is a reserved word), then there is no easy direct way to do it.

For the workaround, take a look at the solutions here - Is there a way to get the enums in VBA?

Upvotes: 2

Related Questions