You can control an enumeration's accessibility outside of its project. This means that you can hide or make it visible outside of its project. To do this, you can precede it with the Private, the Protected, the Friend, or the Public keyword. Here is an example: Module Exercise
Public Enum HouseType
Unknown = 5
SingleFamily = 14
TownHouse
Condominium = 8
End Enum
Public Function Main() As Integer
Dim ht As HouseType
ht = HouseType.SingleFamily
Return 0
End Function
End Module
As mentioned already, once an enumeration has been created, it becomes a type. It can be passed as argument and it can be returned from a function. You pass an enumeration as argument using the same approach of a normal data type. Here is an example: Private Sub ShowHouse(ByVal PropType As HouseType) End Sub In the same way, you can pass as many enumeration types as necessary. In the body of the procedure, you can use the enumeration as you see fit. When calling the procedure, pass an argument that holds a value of the type of enumeration. Here is an example: Module Exercise Public Enum HouseType Unknown = 2 SingleFamily = 4 TownHouse = 6 Condominium = 8 End Enum Private Sub ShowHouse(ByVal PropType As HouseType) MsgBox("Type of house: " & PropType) End Sub Public Function Main() As Integer Dim ht As HouseType ht = HouseType.SingleFamily ShowHouse(ht) Return 0 End Function End Module This would produce:
You can also pass the argument as optional. When creating the procedure, use the Optional keyword to specify that the argument has a default value. When calling the procedure, you can pass or omit passing a value for the argument. Here is an example: Module Exercise Public Enum HouseType Unknown SingleFamily TownHouse Condominium End Enum Private Sub ShowHouse(Optional ByVal PropType As HouseType = HouseType.Unknown) MsgBox("Type of house: " & PropType) End Sub Public Function Main() As Integer Dim ht As HouseType ht = HouseType.SingleFamily ShowHouse() Return 0 End Function End Module This would produce:
To create a function that returns an enumeration, specify its As factor with the name of the enumeration. In the body of the enumeration, do whatever you have to do. Before exiting the function, make sure you return a value that is the type of the enumeration. Here is an example: Private Function SpecifyPropertyType() As HouseType Dim pt As HouseType pt = HouseType.TownHouse Return pt End Function You can call a function that returns an enumeration type by using just its name. Otherwise, you can use its returned value. Here is an example: Module Exercise Public Enum HouseType Unknown SingleFamily TownHouse Condominium End Enum Private Sub ShowHouse(Optional ByVal PropType As HouseType = HouseType.Unknown) MsgBox("Type of house: " & PropType) End Sub Private Function SpecifyPropertyType() As HouseType Dim pt As HouseType pt = HouseType.TownHouse Return pt End Function Public Function Main() As Integer Dim ht As HouseType ht = SpecifyPropertyType() ShowHouse(ht) Return 0 End Function End Module This would produce:
|
|
|||||||
|