VB 11 Tutorial 9: Getting Started
Today we will be learning on how to make our own classes.
Overview: Classes are Objects, when you create a class it is Setup like a Type.
Code:
' Example of Using Classes
Module SubNew
Sub Main()
Dim Tom As New User("Tom124", "Password")
Tom.PassWord = "Password1"
Tom.Save() ' It Only Saves if it has changed.
End Sub
End Module
Public Class User
Private cIsDirty As Boolean
Private cUserName As String
Private cPassWord As String
Public Sub Save()
If cIsDirty Then
' Save to database
End If
End Sub
Sub New(nUserName As String, nPassWord As String)
cUserName = nUserName
cPassWord = nPassWord
End Sub
Public Property UserName As String
Get
Return cUserName
End Get
End Property
Set(value As String)
If value <> cUserName Then
cIsDirty = True
cUserName = value
End If
End Set
End Class
Public Property PassWord As String
Get
Return cPassWord
End Get
End Property
Set(value As String)
If value <> cPassWord Then
cIsDirty = True
cPassWord = value
End If
End Set
End Class
Public Property IsDirtyAs Boolean
Get
Return cIsDirty
End Get
End Property
Set(value As Boolean)
If value <> cIsDirty Then
cIsDirty = value
End If
End Set
End Class
Overview: Classes are Objects, when you create a class it is Setup like a Type.
Code:
' Example of Using Classes
Module SubNew
Sub Main()
Dim Tom As New User("Tom124", "Password")
Tom.PassWord = "Password1"
Tom.Save() ' It Only Saves if it has changed.
End Sub
End Module
Public Class User
Private cIsDirty As Boolean
Private cUserName As String
Private cPassWord As String
Public Sub Save()
If cIsDirty Then
' Save to database
End If
End Sub
Sub New(nUserName As String, nPassWord As String)
cUserName = nUserName
cPassWord = nPassWord
End Sub
Public Property UserName As String
Get
Return cUserName
End Get
End Property
Set(value As String)
If value <> cUserName Then
cIsDirty = True
cUserName = value
End If
End Set
End Class
Public Property PassWord As String
Get
Return cPassWord
End Get
End Property
Set(value As String)
If value <> cPassWord Then
cIsDirty = True
cPassWord = value
End If
End Set
End Class
Public Property IsDirtyAs Boolean
Get
Return cIsDirty
End Get
End Property
Set(value As Boolean)
If value <> cIsDirty Then
cIsDirty = value
End If
End Set
End Class
No comments:
Post a Comment