是否可以在模块级别定义用户定义的类型常量?
Type MyType
name as String
description as String
End Type
' Something like this
Private Const OneType as MyType = "Name" "Description"
答案 0 :(得分:2)
没有。 Const不适用于用户定义的类型。你最接近的是创建一个只有属性获取的类。
Public Property Get Name() As String
Name = "Name"
End Property
Public Property Get Description() As String
Description = "Description"
End Property
如果您希望让具有不同值的类的多个实例但仍希望值保持不变,则添加一个只能使用一次的初始化例程。
Private sName As String
Private sDescription As String
Private Sub Class_Initialize()
sName = ""
End Sub
Public Sub Initialize(Name As String, Description As String)
If Len(sName) = 0 Then
sName = Name
sDescription = Description
Else
MsgBox "This instance of MyClass is already initialized!"
End If
End Sub
Public Property Get Name() As String
Name = sName
End Property
Public Property Get Description() As String
Description = sDescription
End Property
然后声明你的班级实例。
Dim cMyClass1 As New MyClass, cMyClass2 as New MyClass
cMyClass1.Initialize("Name","Description")
cMyClass2.Initialize("DiffName","OtherDescription")