Pushing Electrons
Pages
Home
Resume
Monday, April 4, 2011
Methods Example
The following is an example of how to use a sub and a function in order to reuse code.
Public Class Form1<br /><br /> Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click<br /> Dim str1 As String = TextBox1.Text<br /> Dim strFormatted As String = ""<br /><br /> strFormatted = digitsOnly(str1)<br /> checkNumberLength(strFormatted)<br /> TextBox1.Text = strFormatted<br /><br /> End Sub<br /><br /> Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click<br /> Dim strFormatted As String = "("<br /> TextBox1.Text = digitsOnly(TextBox1.Text)<br /> checkNumberLength(TextBox1.Text)<br /><br /> Dim x As Integer = 1<br /> For Each c As String In TextBox1.Text<br /> If x = 4 Then<br /> strFormatted += ") "<br /> End If<br /> If x = 7 Then<br /> strFormatted += "-"<br /> End If<br /><br /> strFormatted += c<br /> x += 1<br /> Next<br /> TextBox1.Text = strFormatted<br /> End Sub<br /><br /> Private Function digitsOnly(ByVal str As String) As String<br /> Dim strFormatted As String = ""<br /><br /> For Each c As String In str<br /> If Char.IsDigit(c) Then<br /> strFormatted += c<br /> End If<br /> Next<br /> Return strFormatted<br /> End Function<br /><br /> Private Sub checkNumberLength(ByVal num As String)<br /> If num.Length <> 10 Then<br /> MsgBox("Sorry, you need 10 numbers")<br /> End If<br /> End Sub<br />End Class<br />
No comments:
Post a Comment
Newer Post
Older Post
Home
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment