Excel 类范围内的常量UDT
是否可以在模块级别定义用户定义的类型常量Excel 类范围内的常量UDT,excel,vba,Excel,Vba,是否可以在模块级别定义用户定义的类型常量 Type MyType name as String description as String End Type ' Something like this Private Const OneType as MyType = "Name" "Description" 否。Const不适用于用户定义的类型。最接近的方法是创建一个只有属性get的类 Public Property Get Name() As String Nam
Type MyType
name as String
description as String
End Type
' Something like this
Private Const OneType as MyType = "Name" "Description"
否。Const不适用于用户定义的类型。最接近的方法是创建一个只有属性get的类
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")