Advanced VB: Review of the basics


Published on

Published in: Education
  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Advanced VB: Review of the basics

  1. 1. Review of the Basics<br />Advanced Visual Basic<br />
  2. 2. Overview<br />You are now in at least your third semester of programming. In this course, we will be exploring more of Visual Basic. Before doing so, we should re-familiarize ourselves with the development environment and some of the basic programming topics.<br />
  3. 3. Variables<br />You often have to store values when you perform calculations with Visual Basic. For example, you might want to calculate several values, compare them, and perform different operations on them, depending on the result of the comparison. You have to retain the values if you want to compare them.Variables store values in RAM. A variable has a name (the word that you use to refer to the value that the variable contains). A variable also has a data type (which determines the kind of data that the variable can store).<br />Example:<br />Dim strFirstName As String<br />
  4. 4. Variables<br />Research Question: <br />What is the difference between a variable and a constant?<br />
  5. 5. Arrays<br />An array is a set of values that are logically related to each other, such as the number of students in each grade in a grammar school.<br />An array allows you to refer to these related values by the same name and to use a number, called an index or subscript, to tell them apart. The individual values are called the elements of the array. They are contiguous from index 0 through the highest index value.<br />Example:<br />Dim strStudentNames(10) As String<br />
  6. 6. Arrays<br />Research Question: <br />How many elements are in this array?<br />Dim strStudentNames(10) As String<br />
  7. 7. Loops<br />Visual Basic loop structures allow you to run one or more lines of code repetitively. You can repeat the statements in a loop structure until a condition is True, until a condition is False, a specified number of times, or once for each element in a collection.<br />
  8. 8. Loops (while)<br />Use a While...End While structure when you want to repeat a set of statements an indefinite number of times, as long as a condition remains True.<br />Example:<br />Dim counter As Integer = 0 <br />While counter < 3 <br />counter += 1 <br />‘Insert code to use current value of counter. <br />End While <br />MsgBox("While loop ran " & CStr(counter) & " times") <br />
  9. 9. Loops (do)<br />Use a Do...Loop structure when you want to repeat a set of statements an indefinite number of times, until a condition is satisfied.<br />Example:<br />Dim counter As Integer = 0 <br />Dim number As Integer = 10<br />Do Until number = 100 <br /> number = number * 10 <br />counter += 1 <br />Loop <br />MsgBox("The loop ran " & counter & " times.") <br />
  10. 10. Loops (for…next)<br />Use a For...Next structure when you want to repeat a set of statements a set number of times.<br />Example:<br />For index As Integer = 1 To 5 Debug.Write(index.ToString & " ") <br />Next <br />Debug.WriteLine("") ' Output: 1 2 3 4 5 <br />
  11. 11. Loops (for each)<br />Use a For Each...Next loop when you want to repeat a set of statements for each element of a collection or array.<br />Example:<br />Sub BlueBackground(ByValthisFormAs System.Windows.Forms.Form)<br />For Each thisControl As System.Windows.Forms.ControlIn thisForm.Controls<br />thisControl.BackColor= System.Drawing.Color.LightBlue<br />Next thisControl<br />End Sub<br />
  12. 12. Loops<br />Research Question: <br />What happens when a loop doesn’t end?<br /> Dim intValue as integer = 1<br /> Do Until intValue = 2<br />MessageBox.Show(“you have a problem!”)<br /> Loop<br />
  13. 13. Decisions<br />Visual Basic lets you test conditions and perform different operations depending on the results of that test. You can test for a condition being true or false, for various values of an expression, or for various exceptions generated when you execute a series of statements.<br />
  14. 14. Decisions (if…then…else)<br />When an If...Then...Else statement is encountered, condition is tested. If condition is True, the statements following Then are executed. If condition is False, each ElseIf statement (if there are any) is evaluated in order. When a True elseifcondition is found, the statements immediately following the associated ElseIf are executed. If no elseifcondition evaluates to True, or if there are no ElseIf statements, the statements following Else are executed. After executing the statements following Then, ElseIf, or Else, execution continues with the statement following End If.<br />Example:<br />Dim count As Integer = 0 <br />Dim message As String <br />If count = 0 Then <br />message = "There are no items." <br />ElseIfcount = 1 Then <br />message = "There is 1 item." <br />Else <br />message = "There are " & count & " items." <br />End If<br />
  15. 15. Decisions (if…then…else)<br />The Case Else statement is used to introduce the elsestatements to run if no match is found between the testexpression and an expressionlist clause in any of the other Case statements. Although not required, it is a good idea to have a Case Else statement in your Select Case construction to handle unforeseen testexpression values<br />Example:<br />Dim number As Integer = 8 <br />Select Case number <br />Case 1 To 5 <br />Debug.WriteLine("Between 1 and 5, inclusive")<br />Case 6, 7, 8 <br />Debug.WriteLine("Between 6 and 8, inclusive") <br />Case 9 To 10 <br />Debug.WriteLine("Equal to 9 or 10") <br />Case Else <br />Debug.WriteLine("Not between 1 and 10, inclusive") <br />End Select<br />
  16. 16. Decisions<br />Research Question: <br />What is the most efficient way to determine if someone can vote?<br />Consider: <br />Age = 17<br />Age = 18<br />Age = 19<br />
  17. 17. Additional Information<br />For additional information about these topics, please review your text and the links provided in Blackboard.<br />