JayBeeOH

Subroutine And Function Demo - Windows Form

Sep 5th, 2014
304
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 4.64 KB | None | 0 0
  1. '------------------------------------------------------------------------------------------
  2. '           Notice of My Copyright and Intellectual Property Rights
  3. '
  4. ' Any intellectual property contained within the program by Joseph L. Bolen remains the
  5. ' intellectual property of the Joseph L. Bolen. This means that no person may distribute,
  6. ' publish or provide such intellectual property to any other person or entity for any
  7. ' reason, commercial or otherwise, without the express written permission of Joseph L. Bolen.
  8. '
  9. '                 Copyright © 2014. All rights reserved.
  10. '        All trademarks remain the property of their respective owners.
  11. '-------------------------------------------------------------------------------------------
  12. ' Program Name:   Subroutine And Function Demo
  13. ' Author:         Joseph L. Bolen
  14. ' Date Created:   Aug 2014
  15. '
  16. ' Description:    Demonstration of a Subroutine and a Function
  17. '
  18. '                 Documentation is at:
  19. '                   App's screen image is at: http://imgur.com/CpfMsdU
  20. '                   App's Visual Basic .NET code is at http://pastebin.com/FAtyqZSr
  21. '                   Video tutorial at YouTube: http://www.youtube.com/user/bolenpresents
  22. '
  23. '-------------------------------------------------------------------------------------------
  24.  
  25. Option Strict On
  26.  
  27. Public Class MainForm
  28.  
  29. #Region " Class Level Variables"
  30.  
  31.     Private result As String
  32.  
  33. #End Region
  34.  
  35. #Region " Control Procedures"
  36.  
  37.     ' Update output using a subroutine.
  38.     Private Sub SubroutineButton_Click(sender As Object, e As EventArgs) _
  39.         Handles SubroutineButton.Click
  40.  
  41.         result = String.Empty
  42.  
  43.         AddTwoNumbers(FirstIntTextBox.Text, SecondIntTextBox.Text)
  44.  
  45.         SubroutineValueLabel.Text = result
  46.  
  47.  
  48.         ''Using ByRef
  49.  
  50.         'Dim result As String = String.Empty
  51.  
  52.         'AddTwoNumbers(FirstIntTextBox.Text, SecondIntTextBox.Text, result)
  53.  
  54.         'SubroutineValueLabel.Text = result
  55.     End Sub
  56.  
  57.     ' Update output using a function.
  58.     Private Sub FunctionButton_Click(sender As Object, e As EventArgs) _
  59.         Handles FunctionButton.Click
  60.  
  61.         FunctionValueLabel.Text = AddTwoNums(FirstIntTextBox.Text, SecondIntTextBox.Text)
  62.     End Sub
  63.  
  64.     ' Clear all form input and output controls.
  65.     Private Sub ClearButton_Click(sender As Object, e As EventArgs) _
  66.         Handles ClearButton.Click
  67.  
  68.         ' Clear textboxes and value Labels.
  69.         FirstIntTextBox.Clear()
  70.         SecondIntTextBox.Clear()
  71.         SubroutineValueLabel.Text = String.Empty
  72.         FunctionValueLabel.Text = String.Empty
  73.  
  74.         ' Put cursor in first textbox.
  75.         FirstIntTextBox.Focus()
  76.     End Sub
  77.  
  78.  
  79.     ' Clear Value labels if input textboxes change value.
  80.     Private Sub TextBoxes_TextChanged(sender As Object, e As EventArgs) _
  81.         Handles FirstIntTextBox.TextChanged,
  82.                 SecondIntTextBox.TextChanged
  83.  
  84.         SubroutineValueLabel.Text = String.Empty
  85.         FunctionValueLabel.Text = String.Empty
  86.     End Sub
  87. #End Region
  88.  
  89. #Region " Subroutines and Functions"
  90.  
  91.     ' Subroutine to add two integers. Uses class level variable "result".
  92.     Private Sub AddTwoNumbers(ByVal value1 As String, ByVal value2 As String)
  93.  
  94.         Dim num1, num2 As Integer
  95.  
  96.         If Not Integer.TryParse(value1, num1) Then
  97.             result = "First value is not an Integer."
  98.             Exit Sub
  99.         End If
  100.  
  101.         If Not Integer.TryParse(value2, num2) Then
  102.             result = "Second value is not an Integer."
  103.             Exit Sub
  104.         End If
  105.  
  106.         result = (num1 + num2).ToString()
  107.     End Sub
  108.  
  109.     '' Subroutine to add two integers. Uses ByRef for returning parameter.
  110.     'Private Sub AddTwoNumbers(ByVal value1 As String, ByVal value2 As String, ByRef value3 As String)
  111.  
  112.     '    Dim num1, num2 As Integer
  113.  
  114.     '    If Not Integer.TryParse(value1, num1) Then
  115.     '        value3 = "First value is not an Integer."
  116.     '        Exit Sub
  117.     '    End If
  118.  
  119.     '    If Not Integer.TryParse(value2, num2) Then
  120.     '        value3 = "Second value is not an Integer."
  121.     '        Exit Sub
  122.     '    End If
  123.  
  124.     '    value3 = (num1 + num2).ToString()
  125.     'End Sub
  126.  
  127.  
  128.     ' Function to add two integers
  129.     Private Function AddTwoNums(ByVal value1 As String, ByVal value2 As String) As String
  130.  
  131.         Dim num1, num2 As Integer
  132.  
  133.         If Not Integer.TryParse(value1, num1) Then
  134.             Return "First value is not an Integer."
  135.         End If
  136.  
  137.         If Not Integer.TryParse(value2, num2) Then
  138.             Return "Second value is not an Integer."
  139.         End If
  140.  
  141.         Return (num1 + num2).ToString()
  142.     End Function
  143.  
  144. #End Region
  145.  
  146. End Class
Advertisement
Add Comment
Please, Sign In to add comment