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