이 글은 도움말을 번역한 내용입니다.
Property 프로시저는 프로그래머가 사용자 지정 속성을 만들고 조작할 수 있도록 하는 일련의 Visual Basic 문입니다.
Property 프로시저는 양식, 표준 모듈 및 클래스 모듈에 대한 읽기 전용 속성을 만드는 데 사용할 수 있습니다.
속성 값이 설정될 때 실행되어야 하는 코드에서 Public 변수 대신 Property 프로시저를 사용해야 합니다.
Public 변수와 달리 Property 프로시저에는 개체 브라우저에서 도움말 문자열을 할당할 수 있습니다.
속성 프로시저를 만들면 프로시저가 포함된 모듈의 속성이 됩니다. Visual Basic은 다음과 같은 세 가지 유형의 프로퍼티 프로시저를 제공합니다.
Property Let : 속성 값을 설정하는 프로시저입니다.
Property Get : 속성 값을 반환하는 프로시저입니다.
Property Set : 개체에 대한 참조를 설정하는 프로시저입니다.
Property 프로시저를 선언하는 구문은 다음과 같습니다.
[ Public | Private ] [ Static ] Property { Get | Let | Set } propertyname [( arguments )] [ As type ] statements End Property
Property 프로시저는 일반적으로 쌍으로 사용됩니다.
Property Let은 Property Get과 함께 사용되며, Property Set은 Property Get과 함께 사용됩니다
Property Get 프로시저를 단독으로 선언하는 것은 읽기 전용 프로시저를 선언하는 것과 같습니다.
세 가지 Property 프로시저 유형을 모두 함께 사용하는 것은 Variant 변수에만 유용하며, Variant에만 개체 또는 기타 데이터 유형 정보가 포함될 수 있기 때문입니다.
Property Set은 개체와 함께 사용하기 위한 Property이지만 Property Let은 그렇지 않습니다.
Property 프로시저 선언에서 필요한 인수들은 다음 표에 나와 있습니다.
Procedure | Declaration syntax |
Property Get | Property Getpropname (1, …, n) As type |
Property Let | Property Letpropname (1, …,,,, n, n +1) |
Property Set | Property Setpropname (1, …, n, n +1) |
첫 번째 인수부터 마지막 인수(1, ..., n)까지는 이름이 같은 모든 프로퍼티 프로시저에서 동일한 이름과 데이터 유형을 공유해야 합니다.
Property Get 프로시저 선언은 관련 Property Let 및 Property Set 선언보다 인수가 하나 적습니다. Property Get 프로시저의 데이터 유형은 관련 Property Let 및 Property Set 선언의 마지막 인수(n +1)의 데이터 유형과 동일해야 합니다. 예를 들어 다음과 같은 Property Let 프로시저를 선언하는 경우 Property Get 선언은 Property Let 프로시저의 인수와 동일한 이름 및 데이터 유형을 가진 인수를 사용해야 합니다.
Property Let Names(intX As Integer, intY As Integer, varZ As Variant)
' Statement here.
End Property
Property Get Names(intX As Integer, intY As Integer) As Variant
' Statement here.
End Property
Property Set 선언의 마지막 인수의 데이터 유형은 개체 유형 또는 Variant여야 합니다.
Property Let과 Property Get을 사용하는 예제
' 클래스 모듈 이름: MyClass
Private myValue As Double
Public Property Get Value() As Double
Value = myValue
End Property
Public Property Let Value(newValue As Double)
myValue = newValue
End Property
Sub ExampleUsingClassModule()
Dim myClassObj As New MyClass
' Property Let을 사용하여 값 설정
myClassObj.Value = 42.0
' Property Get을 사용하여 값 가져오기
Dim result As Double
result = myClassObj.Value
' 결과 출력
MsgBox "설정된 값: " & myClassObj.Value & vbNewLine & "가져온 값: " & result
End Sub
Property Get만 사용하는 예제
' 클래스 모듈 이름: MyClass
Private myValue As Double
Public Property Get Value() As Double
Value = myValue
End Property
Private Sub Class_Initialize()
' 클래스가 초기화될 때 myValue에 값을 설정합니다.
myValue = 42#
End Sub
Sub ExampleUsingClassModule()
Dim myClassObj As New MyClass
' Value 프로퍼티에서 값을 가져오기
Dim result As Double
result = myClassObj.Value
' 결과 출력
Debug.Print "가져온 값: " & result
End Sub
Property Let과 Property Set을 사용한 예제
' In the "Employee" Class Module
Private nameValue As String
Private hireDateValue As Date
Private employeeCellObject As Range
' Property: Name
Public Property Let Name(ByVal newValue As String)
nameValue = newValue
End Property
' Property: HireDate
Public Property Let HireDate(ByVal newValue As Date)
hireDateValue = newValue
End Property
' Property: EmployeeCell (using Set)
Public Property Set EmployeeCell(ByVal cell As Range)
Set employeeCellObject = cell
End Property
' Method: UpdateEmployeeData
Public Sub UpdateEmployeeData()
If Not employeeCellObject Is Nothing Then
employeeCellObject.value = nameValue
employeeCellObject.Offset(0, 1).value = hireDateValue
End If
End Sub
Sub TestEmployeeClass()
Dim emp As Employee
Set emp = New Employee
' Using Let properties to set employee name and hire date
emp.Name = "John Doe"
emp.HireDate = DateSerial(2023, 7, 15)
' Using Set property to assign a cell for employee data
Dim targetCell As Range
Set targetCell = Sheet1.Range("A1") ' Change the sheet and cell address as needed
Set emp.EmployeeCell = targetCell
' Updating employee data in the specified cell
emp.UpdateEmployeeData
' Clean up
Set emp = Nothing
End Sub
Property Get과 Property Set을 사용한 예제
' In the "CellManager" Class Module
Private targetCellObject As Range
' Property: TargetCell (using Set)
Public Property Set TargetCell(ByVal cell As Range)
Set targetCellObject = cell
End Property
' Property: GetCellValue (read-only)
Public Property Get GetCellValue() As Variant
If Not targetCellObject Is Nothing Then
GetCellValue = targetCellObject.value
Else
GetCellValue = "No cell selected."
End If
End Property
Sub TestCellManagerClass()
Dim cellManager As cellManager
Set cellManager = New cellManager
' Using Set property to assign a Range object
Dim myCell As Range
Set myCell = Sheet1.Range("A1") ' Change the sheet and cell address as needed
Set cellManager.TargetCell = myCell
' Print the cell value using the GetCellValue property
Debug.Print cellManager.GetCellValue ' This will print the value of cell A1 on Sheet1
' Clean up
Set cellManager = Nothing
End Sub
'언어 참조' 카테고리의 다른 글
속성을 설정할 때 코드 실행 (0) | 2023.08.01 |
---|---|
Property 프로시저 호출 (0) | 2023.08.01 |
함수에서 문자열 반환 (0) | 2023.08.01 |
Function 프로시저 작성 (0) | 2023.08.01 |
Sub 프로시저 작성 (0) | 2023.08.01 |