2012-01-26 2 views
0

모듈 수준에서 사용자 정의 형식 상수를 정의 할 수 있습니까?클래스 범위의 상수 UDT

Type MyType 
    name as String 
    description as String 
End Type 

' Something like this 
Private Const OneType as MyType = "Name" "Description" 

답변

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") 
+0

+1 좋은 예 – brettdj

+0

안녕하세요, 감사하겠습니다. – Triztian