Add a Member Field
Let's give our cyborg a name by using a public member field. Then, below, we will change our member field into a VB.Net class property.
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim T1 As New Cyborg
T1.Name = "Number 1"
T1.IntroduceYourself()
End Sub
End Class
Public Class Cyborg
Inherits Object
Public Name
Public Sub IntroduceYourself()
MessageBox.Show("Hi, my name is " + Name + ".")
End Sub
End Class
Member Fields vs Properties
The main difference between a raw member field (as in above) and a property is the formal use of accessor methods. Accessor methods (getters and setters) allow you to control data going into and out of a member field and is considered a best practice.
VB.Net Property Sample Code:
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim T1 As New Cyborg
T1.Name = "Cameron"
T1.IntroduceYourself()
End Sub
End Class
Public Class Cyborg
Private FName As String
Public Property Name()
Get
Return FName
End Get
Set(ByVal value)
FName = value
End Set
End Property
Public Sub IntroduceYourself()
MessageBox.Show("Hi, my name is " + Name + ".")
End Sub
End Class
You can use this class, for example, on the click event of a button:
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim T1 As New Cyborg
T1.Name = "Cameron"
T1.IntroduceYourself()
End Sub
End Class