VB Lecture Notes - Methods
Objective #1: Use a call statement to execute a method from another method.
Objective #2: Create your own method that is not tied to an event.
Public Class Form1
Dim playerX As Integer = 10 ' horizontal position of player Dim playerY As Integer = 10 ' vertical position of player
' draws graphics on screen
Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint DrawPlayer(sender, e) DrawEnemy(sender, e) End Sub
' draws player
Private Sub DrawPlayer(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) e.Graphics.DrawLine(Pens.Black, playerX, playerY, playerX, playerY + 100) End Sub
' draws enemy
Private Sub DrawEnemy(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) e.Graphics.DrawLine(Pens.Red, 200, 50, 200, 150) End Sub
' moves players
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click MoveDiagonal() Me.Refresh() End Sub
' moves player right 10 pixels
Private Sub MoveRight() playerX += 10 End Sub
' moves player down 10 pixels
Private Sub MoveDown() playerY += 10 End Sub
' moves player diagonally down and to the right
Private Sub MoveDiagonal() MoveRight() MoveDown() End Sub
End Class
Objective #3: Explain the benefits of using methods.