Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- '------------------------------------------------------------------------------------------
- ' Notice of My Copyright and Intellectual Property Rights
- '
- ' Any intellectual property contained within the program by Joseph L. Bolen remains the
- ' intellectual property of the Joseph L. Bolen. This means that no person may distribute,
- ' publish or provide such intellectual property to any other person or entity for any
- ' reason, commercial or otherwise, without the express written permission of Joseph L. Bolen.
- '
- ' Copyright © 2014. All rights reserved.
- ' All trademarks remain the property of their respective owners.
- '-------------------------------------------------------------------------------------------
- ' Program Name: Subroutine And Function Demo
- ' Author: Joseph L. Bolen
- ' Date Created: Aug 2014
- '
- ' Description: Demonstration of a Subroutine and a Function
- '
- ' Documentation is at:
- ' App's screen image is at: http://imgur.com/CpfMsdU
- ' App's Visual Basic .NET code is at http://pastebin.com/FAtyqZSr
- ' Video tutorial at YouTube: http://www.youtube.com/user/bolenpresents
- '
- '-------------------------------------------------------------------------------------------
- Option Strict On
- Public Class MainForm
- #Region " Class Level Variables"
- Private result As String
- #End Region
- #Region " Control Procedures"
- ' Update output using a subroutine.
- Private Sub SubroutineButton_Click(sender As Object, e As EventArgs) _
- Handles SubroutineButton.Click
- result = String.Empty
- AddTwoNumbers(FirstIntTextBox.Text, SecondIntTextBox.Text)
- SubroutineValueLabel.Text = result
- ''Using ByRef
- 'Dim result As String = String.Empty
- 'AddTwoNumbers(FirstIntTextBox.Text, SecondIntTextBox.Text, result)
- 'SubroutineValueLabel.Text = result
- End Sub
- ' Update output using a function.
- Private Sub FunctionButton_Click(sender As Object, e As EventArgs) _
- Handles FunctionButton.Click
- FunctionValueLabel.Text = AddTwoNums(FirstIntTextBox.Text, SecondIntTextBox.Text)
- End Sub
- ' Clear all form input and output controls.
- Private Sub ClearButton_Click(sender As Object, e As EventArgs) _
- Handles ClearButton.Click
- ' Clear textboxes and value Labels.
- FirstIntTextBox.Clear()
- SecondIntTextBox.Clear()
- SubroutineValueLabel.Text = String.Empty
- FunctionValueLabel.Text = String.Empty
- ' Put cursor in first textbox.
- FirstIntTextBox.Focus()
- End Sub
- ' Clear Value labels if input textboxes change value.
- Private Sub TextBoxes_TextChanged(sender As Object, e As EventArgs) _
- Handles FirstIntTextBox.TextChanged,
- SecondIntTextBox.TextChanged
- SubroutineValueLabel.Text = String.Empty
- FunctionValueLabel.Text = String.Empty
- End Sub
- #End Region
- #Region " Subroutines and Functions"
- ' Subroutine to add two integers. Uses class level variable "result".
- Private Sub AddTwoNumbers(ByVal value1 As String, ByVal value2 As String)
- Dim num1, num2 As Integer
- If Not Integer.TryParse(value1, num1) Then
- result = "First value is not an Integer."
- Exit Sub
- End If
- If Not Integer.TryParse(value2, num2) Then
- result = "Second value is not an Integer."
- Exit Sub
- End If
- result = (num1 + num2).ToString()
- End Sub
- '' Subroutine to add two integers. Uses ByRef for returning parameter.
- 'Private Sub AddTwoNumbers(ByVal value1 As String, ByVal value2 As String, ByRef value3 As String)
- ' Dim num1, num2 As Integer
- ' If Not Integer.TryParse(value1, num1) Then
- ' value3 = "First value is not an Integer."
- ' Exit Sub
- ' End If
- ' If Not Integer.TryParse(value2, num2) Then
- ' value3 = "Second value is not an Integer."
- ' Exit Sub
- ' End If
- ' value3 = (num1 + num2).ToString()
- 'End Sub
- ' Function to add two integers
- Private Function AddTwoNums(ByVal value1 As String, ByVal value2 As String) As String
- Dim num1, num2 As Integer
- If Not Integer.TryParse(value1, num1) Then
- Return "First value is not an Integer."
- End If
- If Not Integer.TryParse(value2, num2) Then
- Return "Second value is not an Integer."
- End If
- Return (num1 + num2).ToString()
- End Function
- #End Region
- End Class
Advertisement
Add Comment
Please, Sign In to add comment