이 글은 도움말을 번역한 글입니다.
같은 이름을 공유하는 Property Let, Property Set 및 Property Get 프로시저를 만들 수 있습니다. 이렇게 하면 함께 작동하는 관련 프로시저 그룹을 만들 수 있습니다.
예제 코드 - 1
' Person 클래스
Private pName As String
' Property Get 프로시저는 값을 반환합니다.
Property Get Name() As String
Name = pName
End Property
' Property Let 프로시저는 값을 설정합니다.
Property Let Name(ByVal Value As String)
pName = Value
End Property
Sub TestPerson()
Dim P As Person
Set P = New Person
P.Name = "John Doe" ' Property Let 프로시저를 호출하여 값을 설정합니다.
Debug.Print "The person's name is: " & P.Name ' Property Get 프로시저를 호출하여 값을 반환합니다.
End Sub
예제 코드 - 2
' Person 클래스
Private pName As String
' Property Get 프로시저는 값을 반환합니다.
Property Get Name() As String
Name = pName
End Property
' Property Let 프로시저는 값을 설정합니다.
Property Let Name(ByVal value As String)
pName = value
End Property
' Car 클래스
Private pMake As String
Private pOwner As Person
Property Get Make() As String
Make = pMake
End Property
Property Let Make(ByVal value As String)
pMake = value
End Property
Property Get Owner() As Person
Set Owner = pOwner
End Property
Property Set Owner(ByVal value As Person)
Set pOwner = value
End Property
Sub TestCar()
Dim myOwner As Person
Set myOwner = New Person
myOwner.Name = "Jane Doe"
Dim myCar As Car
Set myCar = New Car
myCar.Make = "Toyota"
Set myCar.Owner = myOwner
Debug.Print "The car make is: " & myCar.Make & vbCrLf & _
"The car owner is: " & myCar.Owner.Name
End Sub
Property 프로시저에 이름을 사용한 후에는 해당 이름을 Sub 또는 Function 프로시저, 변수 또는 사용자 정의 유형의 이름에 사용할 수 없습니다.
Property Let 문을 사용하면 속성 값을 설정하는 프로시저를 만들 수 있습니다.
예제 코드 - 3
' Person 클래스
Private pName As String
Property Get Name() As String
Name = pName
End Property
Property Let Name(ByVal value As String)
pName = value
End Property
Sub TestCar()
Dim objPerson As Person
Set objPerson = New Person
' Property Let 프로시저를 사용하여 Name 속성에 값을 설정합니다.
objPerson.Name = "Jane Doe"
' Property Get 프로시저를 사용하여 Name 속성의 값을 반환합니다.
Debug.Print "The person's name is: " & objPerson.Name
End Sub
Property 프로시저를 사용하면 속성 값이 설정되는 동시에 코드를 쉽게 실행할 수 있습니다. Property 프로시저를 사용하여 다음 처리를 수행할 수 있습니다.
속성 값을 설정하기 전에 속성 값을 결정합니다.
예제 코드 - 4
' Person 클래스 모듈 정의
Private pAge As Integer
Public Property Get Age() As Integer
Age = pAge
End Property
Public Property Let Age(ByVal value As Integer)
' 속성 값 결정: 유효한 나이 범위 (0~150)를 확인합니다.
If value >= 0 And value <= 150 Then
pAge = value
Else
Debug.Print "Invalid age entered. Please enter an age between 0 and 150."
End If
End Property
Sub TestPerson()
Dim person1 As New Person
Dim person2 As New Person
' person1의 나이 설정
person1.Age = 30
Debug.Print "Person 1's age: " & person1.Age ' "Person 1's age: 30" 이라는 메시지가 출력됩니다.
' person2의 나이 설정 (유효하지 않은 값: 200)
person2.Age = 200 ' "Invalid age entered. Please enter an age between 0 and 150." 라는 메시지가 출력됩니다.
Debug.Print "Person 2's age: " & person2.Age ' "Person 2's age: 0" 이라는 메시지가 출력됩니다. (기본 값인 0이 출력됩니다.)
End Sub
예제 코드 - 5
' Person 클래스
Private mAge As Integer
' Property Get 프로시저
Public Property Get Age() As Integer
' 나이를 계산하여 반환하는 코드
Age = CalculateAge()
End Property
' 다른 프로시저 또는 함수를 호출하여 나이를 계산하는 예시 함수
Private Function CalculateAge() As Integer
Dim birthYear As Integer
birthYear = 1990 ' 예시로 생년월일을 1990년으로 가정
CalculateAge = Year(Now) - birthYear
End Function
Sub Example()
Dim p As New Person
' Property Get 문을 사용하여 Age 속성 값을 조회합니다.
Debug.Print "Age: " & p.Age ' "Age: 33" 이라는 메시지가 출력됩니다.
End Sub
속성 값이 설정된 후 새 값을 기반으로 합니다.
예제 코드 - 6
'MyCircle 클래스
Private pRadius As Double
Public Property Get Radius() As Double
Radius = pRadius
End Property
Public Property Let Radius(valueToSet As Double)
If valueToSet >= 0 Then
pRadius = valueToSet
Debug.Print "반지름이 설정되었습니다: " & pRadius
Else
Debug.Print "음수 값은 허용되지 않습니다."
Exit Property
End If
End Property
Public Function CircleArea() As Double
CircleArea = WorksheetFunction.Pi() * (pRadius ^ 2)
End Function
Sub TestMyCirclercleArea()
Dim myCircleObj As New MyCircle
myCircleObj.Radius = 5 '첫번째 값을 설정, 지름=5, 면적 계산 후 출력
Debug.Print "반지름이 " & myCircleObj.Radius & "일 때, 원의 면적은 " & myCircleObj.CircleArea & "입니다."
myCircleObj.Radius = 10 '두번째 값을 설정, 지름=10, 면적 계산 후 출력
Debug.Print "반지름이 " & myCircleObj.Radius & "일 때, 원의 면적은 " & myCircleObj.CircleArea & "입니다."
myCircleObj.Radius = -3 '올바르지 않은 값을 설정
End Sub
도움말 출처
Executing code when setting properties (VBA)
Office VBA reference topic
learn.microsoft.com
'언어 참조' 카테고리의 다른 글
명명된 인수 및 선택적 인수 이해 (0) | 2023.08.03 |
---|---|
배열 선언 및 사용 (0) | 2023.08.01 |
Property 프로시저 호출 (0) | 2023.08.01 |
Property 프로시저 작성 (0) | 2023.08.01 |
함수에서 문자열 반환 (0) | 2023.08.01 |