Monday, September 27, 2010

Monday, September 27th 2010

I did this tutorial on functions and factorials based off of a YouTube video here mkaatr's channel




Function Factorial(ByVal V As Double) As Double
Dim F As Double
Dim I As Integer
F = 1
For I = 1 To V
F = F * I

Next
Return F
End Function

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim number As Double
number = CDbl(TextBox1.Text)
number = CStr(Factorial(number))
txtFactorials.Text = number
End Sub


I also did another tutorial based on ByVal and ByRef



Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim number As Integer = 50
MsgBox(number)
Call byValTest(number)
MsgBox(number)
End Sub

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Dim number As Integer = 50
MsgBox(number)
Call byRefTest(number)
MsgBox(number)
End Sub


There was also a tutorial on creating a swap function




Function swap(ByRef V1 As Integer, ByRef V2 As Integer)
Dim tmp As Integer
tmp = V1
V1 = V2
V2 = tmp
End Function


There is also a quite lengthy tutorial on subroutines from mkaatr



Public Class Form1
Dim names As New Collection
Dim tels As New Collection

Sub addContact(ByVal CName As String, ByVal CTel As String)
names.Add(CName)
tels.Add(CTel)
End Sub

Sub viewContacts(ByVal DGV As DataGridView)
DGV.Rows.Clear()
Dim I As Integer
For I = 1 To names.Count
DGV.Rows.Add(names(I), tels(I))
Next
End Sub


Function GetTelForName(ByVal Name As String) As String
Dim i As Integer
For i = 1 To names.Count
If names(i) = Name Then
Return tels(i)
End If
Next
Return ""
End Function


Private Sub DataGridView1_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick

End Sub

Private Sub AddNewToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AddNewToolStripMenuItem.Click

Dim n As String
Dim t As String

n = InputBox("Enter the name of the contact:")
If n = "" Then
Exit Sub
End If

t = InputBox("Enter the tel number:")
If t = "" Then
Exit Sub
End If

addContact(n, t)
viewContacts(DataGridView1)

End Sub




Private Sub SearchToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SearchToolStripMenuItem.Click
Dim n As String
Dim t As String

n = InputBox("Enter the name you are searching for:")
If n = "" Then
Exit Sub
End If

t = GetTelForName(n)
If t = "" Then
MsgBox("Name not found")
Else
MsgBox("The telephone number is:" & t)
End If
End Sub
End Class

No comments: