Advertisement
Guest User

Untitled

a guest
Jul 15th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.14 KB | None | 0 0
  1. ' You can create multiline Text Boxes by setting Multiline to True
  2. ' Scrollbars to Vertical and Wordwrap to True
  3.  
  4. Public Class Form1
  5. Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
  6.  
  7. End Sub
  8.  
  9. ' Private means that this subroutine can't be called
  10. ' by any code outside of this class
  11. ' Sub means that this method doesn't return a value
  12. ' btnUpdate_Click is the name of the subroutine
  13. ' Values sent to this are between parentheses
  14. ' Handles defines that this is called when the even triggers
  15. Private Sub btnUpdate_Click(sender As Object, e As EventArgs) Handles btnUpdate.Click
  16.  
  17. ' If you don't assign a type a default will be chosen
  18. ' based on the value assigned
  19. Dim randomNumber = 12.2
  20. TextBox1.Text = TypeName(randomNumber)
  21.  
  22. ' ---------- Math Functions ----------
  23.  
  24. ' You join strings with other data with &
  25. ' Environment.NewLine adds the platform specific newline
  26. Dim txtOutput As String = "Abs(-19) = " & Math.Abs(-19) & Environment.NewLine
  27.  
  28. ' &= is a shortcut you can use to join a string with
  29. ' the original string
  30. txtOutput &= "Ceiling(4.5) = " & Math.Ceiling(4.5) & Environment.NewLine
  31.  
  32. txtOutput &= "Floor(4.5) = " & Math.Floor(4.5) & Environment.NewLine
  33.  
  34. ' 2.718 e raised to the specified power
  35. txtOutput &= "Exp(1) = " & Math.Exp(1) & Environment.NewLine
  36.  
  37. ' Return the natural log
  38. txtOutput &= "Log(2.718) = " & Math.Log(2.718) & Environment.NewLine
  39.  
  40. ' Return base 10 log
  41. txtOutput &= "Log10(3000) = " & Math.Log10(3000) & Environment.NewLine
  42.  
  43. ' Return the larger number
  44. txtOutput &= "Max(5,4) = " & Math.Max(5, 4) & Environment.NewLine
  45.  
  46. ' Return the smaller number
  47. txtOutput &= "Min(5,4) = " & Math.Min(5, 4) & Environment.NewLine
  48.  
  49. ' Return the number to the power of number
  50. txtOutput &= "Pow(5,2) = " & Math.Pow(5, 2) & Environment.NewLine
  51.  
  52. ' Return the sqrt
  53. txtOutput &= "Sqrt(25) = " & Math.Sqrt(25) & Environment.NewLine
  54.  
  55. ' Round a Decimal or Double
  56. txtOutput &= "Round(4.5) = " & Math.Round(4.5) & Environment.NewLine
  57.  
  58. ' There is also Cos, Sin, Tan, Acos, Asin, Atan, Cosh, Sinh
  59. ' Tanh
  60.  
  61. ' You can add, subtract, multiply and divide a number
  62. ' and save it back to itself
  63. Dim randInt = 10
  64. randInt += 10
  65. txtOutput &= "10 + 10 = " & randInt & Environment.NewLine
  66.  
  67. ' If you save to an integer it stays an integer
  68. randInt *= 0.13
  69. txtOutput &= "20 * .13 = " & randInt & Environment.NewLine
  70.  
  71. ' TextBox1.Text = txtOutput
  72.  
  73. ' ---------- Strings ----------
  74.  
  75. txtOutput = ""
  76.  
  77. Dim randStr As String = "This is a string"
  78.  
  79. ' Get the length of a String
  80. txtOutput &= "randStr Length = " & randStr.Length & Environment.NewLine
  81.  
  82. ' Start at index 0 and return the next 3 letters
  83. txtOutput &= "1st 3 = " & randStr.Substring(0, 3) & Environment.NewLine
  84.  
  85. ' Replace a string
  86. randStr = randStr.Replace("string", "sentence")
  87. txtOutput &= "Changed String = " & randStr & Environment.NewLine
  88.  
  89. ' Formatting a Decimal
  90. Dim decRandNum As Decimal = 3123.14159
  91.  
  92. ' Add thousands separator and only 3 decimals
  93. txtOutput &= "Pi = " & String.Format("{0:n3}", decRandNum) & Environment.NewLine
  94.  
  95. ' Add thosands separator and treat as currency
  96. txtOutput &= "Pi Currency = " & String.Format("{0:c}", decRandNum) & Environment.NewLine
  97.  
  98. ' Display 5 numbers before the decimal and 1 after
  99. txtOutput &= "Pi Random = " & String.Format("{0:00000.0}", decRandNum) & Environment.NewLine
  100.  
  101. ' g Displays without thousands separator
  102. ' f Displays with at least 1 number on the left and right
  103. ' of the decimal
  104. ' p Multiplies the number times 100 and shows %
  105. ' e Display in exponential notation
  106.  
  107. ' Get the 1st location of a string
  108. ' Define 1st character to start with, string to search,
  109. ' string to search for
  110. txtOutput &= "is in String = " & InStr(1, randStr, "i", CompareMethod.Text) & Environment.NewLine
  111.  
  112. ' Create a fixed size array
  113. ' Arrays are used to store multiple values
  114. Dim arrayEmployees(0 To 2) As String
  115. arrayEmployees(0) = "Bob"
  116. arrayEmployees(1) = "Sally"
  117. arrayEmployees(2) = "Paul"
  118.  
  119. ' Join array values in a string
  120. Dim strEmployees As String = Join(arrayEmployees, ", ")
  121.  
  122. txtOutput &= "Employees = " & strEmployees & Environment.NewLine
  123.  
  124. ' Split a string into an array
  125. arrayEmployees = Split(strEmployees, ", ")
  126.  
  127. txtOutput &= "Employees = " & arrayEmployees.ToString & Environment.NewLine
  128.  
  129. ' For loop for cycling through an array
  130. ' i increments each time through the loop until
  131. ' its value is arrayEmployees.Length - 1
  132. For i As Integer = 0 To arrayEmployees.Length - 1
  133. txtOutput &= "Employee = " & arrayEmployees(i) & Environment.NewLine
  134. Next
  135.  
  136. ' Return the left 3 characters
  137. txtOutput &= "Left 3 = " & Strings.Left(randStr, 3) & Environment.NewLine
  138.  
  139. ' Return the right 3 characters
  140. txtOutput &= "Right 3 = " & Strings.Right(randStr, 3) & Environment.NewLine
  141.  
  142. ' Return uppercase
  143. txtOutput &= "Uppercase = " & UCase(randStr) & Environment.NewLine
  144.  
  145. ' Return lowercase
  146. txtOutput &= "Lowercase = " & LCase(randStr) & Environment.NewLine
  147.  
  148. ' Reverse string
  149. txtOutput &= "Reverse = " & StrReverse(randStr) & Environment.NewLine
  150.  
  151. ' Compare 2 strings
  152. ' Returns -1 if 1 sorts ahead of 2
  153. ' Returns 0 if they are equal
  154. ' Returns 1 if 1 sorts after 2
  155. txtOutput &= "Dog Compare Cat = " & StrComp("Dog", "Cat", CompareMethod.Text) & Environment.NewLine
  156.  
  157. ' Trim whitespace
  158. ' There is also LTrim and RTrim
  159. txtOutput &= "Trim = " & Trim(" string ") & Environment.NewLine
  160.  
  161. ' ---------- Dates ----------
  162.  
  163. txtOutput = ""
  164.  
  165. ' Create a date with the current time
  166. Dim dteCurrent As Date = Now
  167.  
  168. ' Get the month
  169. txtOutput &= "Month = " & dteCurrent.Month & Environment.NewLine
  170.  
  171. ' Get the day
  172. txtOutput &= "Day = " & dteCurrent.Day & Environment.NewLine
  173.  
  174. ' Get the year
  175. txtOutput &= "Year = " & dteCurrent.Year & Environment.NewLine
  176.  
  177. ' Get the hour
  178. txtOutput &= "Hour = " & dteCurrent.Hour & Environment.NewLine
  179.  
  180. ' Get the minute
  181. txtOutput &= "Minute = " & dteCurrent.Minute & Environment.NewLine
  182.  
  183. ' Get the second
  184. txtOutput &= "Second = " & dteCurrent.Second & Environment.NewLine
  185.  
  186. ' Get the day name
  187. txtOutput &= "Day = " & dteCurrent.ToString("dddd") & Environment.NewLine
  188.  
  189. ' Get the day name
  190. txtOutput &= "Month = " & dteCurrent.ToString("MMMM") & Environment.NewLine
  191.  
  192. ' Set a date
  193. dteCurrent = #12/21/1974 11:32:00 AM#
  194.  
  195. ' Get the whole time and date
  196. txtOutput &= "Date = " & dteCurrent.ToLongTimeString & dteCurrent.ToLongDateString & Environment.NewLine
  197.  
  198. ' ---------- Methods ----------
  199. ' We use functions to promote code reuse and to break
  200. ' code into parts so it is easier to understand
  201. ShowMessage()
  202.  
  203. ' Call a function
  204. txtOutput &= "6 + 5 = " & GetSum(6, 5) & Environment.NewLine
  205.  
  206. ' Pass a variable to a function
  207. Dim intVal As Integer = 10
  208.  
  209. ' The value and not the variable is passed to a function
  210. ' so what happens to it in the function is temporary
  211. ChangeVal(intVal)
  212.  
  213. txtOutput &= "intVal out of Func 1 = " & intVal & Environment.NewLine
  214.  
  215. ChangeVal2(intVal)
  216.  
  217. txtOutput &= "intVal out of Func 2 = " & intVal & Environment.NewLine
  218.  
  219. txtOutput &= "1 + 2 + 3 + 4 = " & GetSumMore(1, 2, 3, 4) & Environment.NewLine
  220.  
  221. TextBox1.Text = txtOutput
  222.  
  223. End Sub
  224.  
  225. ' Create a subroutine that opens a MessageBox
  226. Private Sub ShowMessage()
  227. MessageBox.Show("Hello Again", "Love Saying Hello")
  228. End Sub
  229.  
  230. ' Functions can return values
  231. Private Function GetSum(num1 As Integer, num2 As Integer)
  232. Return num1 + num2
  233. End Function
  234.  
  235. Private Sub ChangeVal(intVal As Integer)
  236. intVal = 20
  237. MessageBox.Show("intVal in Func 1 = " & intVal, "Change Value")
  238. End Sub
  239.  
  240. ' You can define that you want to be able to change the value
  241. ' outside of a function with ByRef
  242. Private Sub ChangeVal2(ByRef intVal As Integer)
  243. intVal = 30
  244. MessageBox.Show("intVal in Func 2 = " & intVal, "Change Value")
  245. End Sub
  246.  
  247. ' You can accept a variable number of parameters
  248. Private Function GetSumMore(ByVal ParamArray Numbers() As Integer)
  249. Dim sum As Integer = 0
  250.  
  251. For i As Integer = 0 To Numbers.Length
  252. sum += i
  253. Next
  254.  
  255. Return sum
  256.  
  257. End Function
  258.  
  259. End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement