JayBeeOH

Subroutine And Function Demo - Console App

Aug 29th, 2014
345
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 3.18 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 - Console App
  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 Visual Basic .NET code is at http://pastebin.com/ZyhdwWV5
  20. '
  21. Module Module1
  22.     Private result As String
  23.     Private aKey As ConsoleKeyInfo
  24.  
  25.     Sub Main()
  26.  
  27.         Do
  28.             Console.Clear()
  29.  
  30.             Dim num1String As String = InputBox("Input first whole number", "Adding Two Numbers", "0")
  31.             If num1String.Length = 0 Then num1String = "0"
  32.             Dim num2String As String = InputBox("Input second whole number", "Adding Two Numbers", "0")
  33.             If num2String.Length = 0 Then num2String = "0"
  34.  
  35.             ' Call the Subroutine
  36.             AddTwoNumbers(num1String, num2String)
  37.             Console.WriteLine("The sum of " & num1String.Trim & " and " & num2String.Trim & " using a Subroutine is: " & result)
  38.  
  39.             ' Use the Function
  40.             Console.WriteLine()
  41.             Console.WriteLine("The sum of " & num1String.Trim & " and " & num2String.Trim & " using a Function is: " & AddToNums(num1String, num2String))
  42.  
  43.             ' Hold console open until key is pressed.
  44.             Console.WriteLine()
  45.             Console.WriteLine("Press the ""Q"" key to end program.")
  46.             aKey = Console.ReadKey()
  47.         Loop While (aKey.Key <> ConsoleKey.Q)
  48.  
  49.     End Sub
  50.  
  51.     Private Sub AddTwoNumbers(ByVal pString1 As String, ByVal pString2 As String)
  52.         Dim num1, num2 As Integer
  53.         If Not Integer.TryParse(pString1, num1) Then
  54.             result = vbCrLf & "Error: First value is not a whole number."
  55.             Exit Sub
  56.         End If
  57.         If Not Integer.TryParse(pString2, num2) Then
  58.             result = vbCrLf & "Error: Second value is not a whole number."
  59.             Exit Sub
  60.         End If
  61.         result = (num1 + num2).ToString("n0")
  62.     End Sub
  63.  
  64.     Private Function AddToNums(ByVal pString1 As String, ByVal pString2 As String) As String
  65.         Dim num1, num2 As Integer
  66.         If Not Integer.TryParse(pString1, num1) Then
  67.             Return vbCrLf & "Error: First value is not a whole number."
  68.         End If
  69.         If Not Integer.TryParse(pString2, num2) Then
  70.             Return vbCrLf & "Error: Second value is not a whole number."
  71.         End If
  72.         Return (num1 + num2).ToString("n0")
  73.     End Function
  74. End Module
Advertisement
Add Comment
Please, Sign In to add comment