[VB.NET] 자동 구현 속성(Property Examples (Get, Set)) - Dot Net(Visual Basic) > vb.net

본문 바로가기

vb.net

[기타] [VB.NET] 자동 구현 속성(Property Examples (Get, Set)) - Dot Net(Visual Basic…

회원사진
하나를하더라도최선을
2020-02-22 20:46 9,545 0

본문



Public Property Name As String
Public Property Owner As String = "DefaultName"
Public Property Items As New List(Of String) From {"M""T""W"}
Public Property ID As New Guid()
 
다음 코드 예제에서는 앞의 자동 구현 속성 예제에 해당하는 코드를 보여 줍니다.
Private _Prop2 As String = "Empty"
Property Prop2 As String
    Get
        Return _Prop2
    End Get
    Set(ByVal value As String)
        _Prop2 = value
    End Set
End Property
 
 
다음 코드에서는 읽기 전용 속성 구현을 보여 줍니다.
Class Customer  
   Public ReadOnly Property Tags As New List(Of String)  
   Public ReadOnly Property Name As String = ""  
   Public ReadOnly Property File As String  
  
   Sub New(file As String)  
      Me.File = file  
   End Sub  
End Class
 
 
지원 필드에는 또한 다음과 같은 특징이 있습니다.
속성 자체에 Private과 같은 다른 액세스 수준이 있는 경우에도, 지원 필드에 대한 액세스 한정자는 항상 Public입니다.
속성이 Shared로 표시된 경우 지원 필드도 공유됩니다.
속성에 대해 지정된 특성은 지원 필드에 적용되지 않습니다.
클래스 내의 코드에서, 그리고 조사식 창과 같은 디버깅 도구에서 지원 필드에 액세스할 수 있습니다. 그러나 지원 필드는 IntelliSense 단어 완성 목록에 표시되지 않습니다.
 
 
자동 구현 속성 확장명
자동 구현 속성을 Get 또는 Set 프로시저가 포함된 확장된 속성으로 변환해야 하는 경우 Visual Basic 코드 편집기는 속성에 대한 Get 및 Set 프로시저와 End Property 문을 자동으로 생성할 수 있습니다. 
Property 문 뒤에 있는 빈 줄에 커서를 놓고 G (Get) 또는 S (Set)를 입력 한 다음 ENTER 키를 누르면 코드가 생성 됩니다. 
Get 문 끝에서 Enter 키를 누르면 Visual Basic 코드 편집기에서 읽기 전용 및 쓰기 전용 속성에 대한 Set 또는 Property 프로시저가 자동으로 생성됩니다.

VB.NET program that uses property syntax
 
Class Example
    Private _count As Integer
 
    Public Property Number() As Integer
        Get
            Return _count
        End Get
        Set(ByVal value As Integer)
            _count = value
        End Set
    End Property
End Class
 
Module Module1
    Sub Main()
        Dim e As Example = New Example()
        ' Set property.
        e.Number = 1
        ' Get property.
        Console.WriteLine(e.Number)
    End Sub
End Module
 
Output
 
1
 
 
 
VB.NET program that causes compile-time error
 
Module Module1
    Property Answer() As Integer
        Get
            Return 42
        End Get
    End Property
 
    Sub Main()
        ' Program won't compile.
    End Sub
End Module
 
Output
 
Error BC30124
Property without a 'ReadOnly' or 'WriteOnly' specifier must provide both a 'Get' and a 'Set'.
 
 
 
VB.NET program that uses ReadOnly Property
 
Class Example
    Public ReadOnly Property Count() As Integer
        Get
            Return 500
        End Get
    End Property
End Class
 
Module Module1
    Sub Main()
        Dim e As Example = New Example()
        Console.WriteLine(e.Count)
    End Sub
End Module
 
Output
 
500
 
 
VB.NET program that uses WriteOnly keyword
 
Class Example
    Dim _id As Integer
 
    Public WriteOnly Property Id
        Set(value)
            ' Sets the field from an external call.
            _id = value
        End Set
    End Property
 
    Public Sub Display()
        Console.WriteLine(_id)
    End Sub
End Class
 
Module Module1
    Sub Main()
        ' Create Example and assign Id.
        Dim e As Example = New Example
        e.Id = 100
        e.Display()
    End Sub
End Module
 
Output
 
100
 
 
Program, auto-implemented properties: VB.NET
 
Class Dog
    Public Property Name As String
    Public Property Weight As Integer = 10
End Class
 
Module Module1
 
    Sub Main()
        ' Use automatically-implemented properties.
        Dim dog As Dog = New Dog()
        dog.Name = "Old Yeller"
        dog.Weight = 50
 
        ' Print values.
        Console.WriteLine(dog.Name)
        Console.WriteLine(dog.Weight)
    End Sub
 
End Module
 
Output
 
Old Yeller
50
 

댓글목록0

등록된 댓글이 없습니다.
게시판 전체검색