VB.Net Tutorial/Class Module/Member Fields

Материал из VB Эксперт
Перейти к: навигация, поиск

Field initialization

Imports System
Module Test
  Sub Main()
    Dim obj As YourClass
    obj = New YourClass()
    Console.WriteLine("Name is: " & obj.Name)
    Console.WriteLine("Address is: " & obj.Address)
    Console.WriteLine("ID is: " & obj.ID)
  End Sub
End Module
Public Class YourClass
  Public Name As String = "New YourClass"
  Public Address As String
  Public ID As Integer
End Class
Name is: New YourClass
Address is:
ID is: 0

Public Field

Imports System
Module PointPublic
  Sub Main()
    Dim p as Point = New Point()
    Console.WriteLine("(" & p.X & "," & p.Y & ")") 
    p.X = -100
    p.Y = -100
    Console.WriteLine("(" & p.X & "," & p.Y & ")") 
  End Sub
End Module
Public Class Point
  Public Y as Integer  "Public Field
  Public X as Integer  "Public Field
End Class
(0,0)
(-100,-100)

Shadow fields

Public Class Tester
    Public Shared Sub Main
        Dim objDerivedClass As New DerivedClass
        Console.WriteLine(objDerivedClass.Value)
    End Sub
End Class
Class BaseClass
    Public Value As Double = 3.14
End Class
Class DerivedClass
    Inherits BaseClass
    Public Shadows ReadOnly Property Value() As Double
        Get
            Return 2.7
        End Get
    End Property
End Class
2.7

Shadow member variable in base class

Module Module1
    Class BaseClass
        Public Name As String
        Public Amount As Integer
        Public Message As String
        Public Sub New(ByVal Name As String, ByVal Amount As Integer, ByVal Message As String)
            Me.Name = Name
            Me.Amount = Amount
            Me.Message = Message
        End Sub
        Public Sub ShowBaseMembers()
            Console.WriteLine("Name: " & Name)
            Console.WriteLine("Amount: " & Amount)
            Console.WriteLine("Message: " & Message)
        End Sub
    End Class
    Class DerivedClass
        Inherits BaseClass
        Public Amount As Double
        Public Shadows Message As String
        Public Sub ShowDerivedMembers()
            Console.WriteLine("Name: " & Name)
            Console.WriteLine("Amount: " & Amount)
            Console.WriteLine("Message: " & Message)
        End Sub
        Public Sub New(ByVal Name As String, ByVal Amount As Integer, ByVal Message As String)
            MyBase.New(Name, Amount, Message)
        End Sub
    End Class
    Sub Main()
    End Sub
End Module