Podcast
Questions and Answers
Which data types can the expression in a Select Case
statement evaluate to in VB.Net?
Which data types can the expression in a Select Case
statement evaluate to in VB.Net?
- Only numeric types (Integer, Double)
- Only Boolean and Date types
- Only String and Char types
- Any elementary data type including Boolean, Byte, Char, Date, and String (correct)
In a Select Case
statement, the Case Else
block is executed when none of the preceding Case
conditions are met.
In a Select Case
statement, the Case Else
block is executed when none of the preceding Case
conditions are met.
True (A)
What property of a ListBox control needs to be modified to prevent partial item display?
What property of a ListBox control needs to be modified to prevent partial item display?
IntegralHeight
Multiple expression clauses in a Select Case
statement are separated by __________.
Multiple expression clauses in a Select Case
statement are separated by __________.
Match the loop types with their descriptions:
Match the loop types with their descriptions:
What is the primary purpose of a Select Case
statement?
What is the primary purpose of a Select Case
statement?
Setting the Dock
property of a ListBox to Fill
makes it occupy the entire available space of its parent container.
Setting the Dock
property of a ListBox to Fill
makes it occupy the entire available space of its parent container.
What event is typically used to handle user selection from a ListBox?
What event is typically used to handle user selection from a ListBox?
In VB.NET, the keyword used to specify a decrement in a For
loop is _______.
In VB.NET, the keyword used to specify a decrement in a For
loop is _______.
In the context of loops, what distinguishes a For
loop from a Do
loop?
In the context of loops, what distinguishes a For
loop from a Do
loop?
Which property is used to set the text displayed on a Windows Forms button?
Which property is used to set the text displayed on a Windows Forms button?
In a For Each
loop, the loop variable must be explicitly incremented.
In a For Each
loop, the loop variable must be explicitly incremented.
What method is used to add items to a ListBox control?
What method is used to add items to a ListBox control?
When adding items into a ListBox from your C drive, My.Computer.FileSystem.GetDirectories
requires the ________ path as parameter.
When adding items into a ListBox from your C drive, My.Computer.FileSystem.GetDirectories
requires the ________ path as parameter.
What is the purpose of the Clear()
method when used with the Items
property of a ListBox?
What is the purpose of the Clear()
method when used with the Items
property of a ListBox?
A Do Until
loop executes its block of code as long as the condition is true.
A Do Until
loop executes its block of code as long as the condition is true.
In VB.NET, what function is used to generate a random number?
In VB.NET, what function is used to generate a random number?
In a Do While
loop, the code block is executed as long as the specified condition remains __________.
In a Do While
loop, the code block is executed as long as the specified condition remains __________.
After the following code is executed, what will be the final displayed message?
After the following code is executed, what will be the final displayed message?
Match example list box entries, to their associated gender:
Match example list box entries, to their associated gender:
Flashcards
Select Case Statement
Select Case Statement
A statement that allows a variable to be tested for equality against a list of values. Each value is called a case.
Expression (Select Case)
Expression (Select Case)
An expression that must evaluate to any of the elementary data types in VB.Net.
Expression List
Expression List
A list of expression clauses representing match values for the main expression, separated by commas.
Statements (Select Case)
Statements (Select Case)
Signup and view all the flashcards
Else Statements
Else Statements
Signup and view all the flashcards
For Loops
For Loops
Signup and view all the flashcards
Do Loops
Do Loops
Signup and view all the flashcards
For...Next Loop with Step
For...Next Loop with Step
Signup and view all the flashcards
Looping Backward
Looping Backward
Signup and view all the flashcards
For Each Loop
For Each Loop
Signup and view all the flashcards
Do Until Loop
Do Until Loop
Signup and view all the flashcards
Do While Loop
Do While Loop
Signup and view all the flashcards
Study Notes
- Lecture three covers Select Case statements and Loops.
Select Case
- Enables a variable to be tested for equality against a list of values.
- Each value constitutes a case.
- The variable being switched on is checked for each select case.
- Syntax:
- Select Case expressionCase value1
- Case value1
- Statements
- Case value2
- Statements
- Case Else
- Statements
- End Select
- Expression must evaluate to any elementary data type in VB.Net like Boolean, Byte, Char, Date, Double, Decimal, Integer, Long, Object, SByte, Short, Single, String, UInteger, ULong, and UShort.
- Expressionlist is a list of expression clauses, which represent matching values for the expression.
- Multiple expression clauses are separated by commas.
- Statements are the code that runs following the case that runs if the select expression matches any clause in the expression list.
- Else statements are the code that runs following the default Case, if the select expression does not match any clause in the expressionlist of any of the Case statements.
- Example program creates a Windows Forms Application project called Select Demo.
- Sets the Text property of the form to Select Case.
- Adds a ListBox control named 1stData with IntegralHeight set to False and Dock to Fill.
- Fills the 1stData listbox with the names: Omer, Ahmed, Ali, and Noor.
- The program displays a message box indicating the person's favorite color depending on the name selected in the listbox.
- VB.NET code example:
- Private Sub lstData_SelectedIndexChanged(sender As Object, e As EventArgs) Handles lstData.SelectedIndexChanged
- Dim strName, strFavoriteColor As String
- strName = lstData.Items(lstData.SelectedIndex).ToString
- Select Case strName
- Case "Omer": strFavoriteColor = "Yellow"
- Case "Ahmed": strFavoriteColor = "Blue"
- Case "Ali": strFavoriteColor = "Green"
- Case "Noor": strFavoriteColor = "white"
- End Select
- MessageBox.Show(strName & "'s favorite color is " & strFavoriteColor, "Select Demo")
- End Sub
- The select case statement can also be used to display a person's gender depending on the name selected.
- Example VB.NET code:
- Select Case strName.ToLower
- Case "Omer", “Ahmed", “Ali": MessageBox.Show("This person's gender is male.", "Select Demo")
- Case “Noor", “Rana": MessageBox.Show("This person's gender is female.", "Select Demo")
- End Select
- Example VB.NET code:
- The Case Else statement can be used to catch all remaining unmatched cases.
- Example VB.NET code:
- Select Case strName
- Case “Ahmed", “Omer": MessageBox.Show("This person's gender is male.", "Select Demo")
- Case “Noor", “Teba", “Zainab": MessageBox.Show("This person's gender is female.", "Select Demo")
- Case Else: MessageBox.Show("I don't know this person's gender.", "Select Demo")
- End Select
- Example VB.NET code:
Loops
- When writing computer software, you often need to perform the same task several times.
- For example: creating a telephone bill for all customers or reading in ten files from your computer's disk.
- For Loops: These loops occur a certain number of times, like ten times.
- Do Loops: These loops keep running until a certain condition is reached, like until all the data is processed.
- For...Next Loop: loops a specific number of iterations
- Create a new Windows Forms Application project called Loops.
- Add a ListBox and a Button control to the form.
- Change the Name property of the ListBox to 1stData and its IntegralHeight property to False.
- Change the Name property of the button to btnForNextLoop.
- Set its Text property to For NextLoop.
- Double-click the button to create its Click event handler and add the following code:
- Private Sub btnForNextLoop_Click(sender As Object, e As EventArgs) Handles btnForNextLoop.Click
- Dim intCount As Integer
- lstData.Items.Clear()
- For intCount = 1 Το 5
- lstData.Items.Add("I'm item " & intCount.ToString & " in the list!")
- Next
- End Sub
- For...Next Loop with Step: loops a specific number of iterations with a specified increment.
- Return to the Forms Designer for the Loops project.
- Add a Button control to your form.
- Set its Name property to btnForNextLoopWithStep.
- Set its Text property to For Next Loop w/ Step.
- Double-click the button and add the following code in the Click event handler:
- Private Sub btnForNextLoopWithStep_Click(sender As Object, e As EventArgs) Handles btnForNextLoopWithStep.Click
- lstData.Items.Clear()
- For intCount As Integer = 4 Το 62 Step 7
- lstData.Items.Add(intCount.ToString)
- Next
- End Sub
- Looping Backward: loops in reverse order
- Return to the Forms Designer and add another Button control to your form.
- Set its Name property to btnBackwardsForNextLoop.
- Set its Text property to Backwards For Next Loop.
- Double-click the button and add the following code in the Click event handler:
- Private Sub btnBackwardsForNextLoop_Click(sender As Object, e As EventArgs) Handles btnBackwardsForNextLoop.Click
- lstData.Items.Clear()
- For intCount As Integer = 10 Το 1 Step -1
- lstData.Items.Add(intCount.ToString)
- Next
- End Sub
- For Each Loop: loops through each item in a collection.
- Return to the Forms Designer, add another Button control to your form.
- Set its Name property to btnForEachLoop and its Text property to For Each Loop.
- Double-click the button and add the following code in the Click event handler:
- Private Sub btnForEachLoop_Click(sender As Object, e As EventArgs) Handles btnForEachLoop.Click
- lstData.Items.Clear()
- For Each strFolder As String In My.Computer.FileSystem.GetDirectories("C:")
- 1stData.Items.Add(strFolder)
- Next
- End Sub
- Do Until...Loop: executes a block of code until a condition becomes true.
- Return to the Forms Designer in the Loops project, add another Button control to your form.
- set its Name property to btnDoUntilLoop.
- set its Text property to Do Until Loop.
- Double-click the button and add the following code to its Click event handler:
- Private Sub btnDoUntilLoop_Click(sender As Object, e As EventArgs) Handles btnDoUntilLoop.Click
- Dim objRandom As New Random
- Dim intRandomNumber As Integer = 0
- 1stData.Items.Clear()
- Do Until intRandomNumber = 10
- intRandomNumber = objRandom.Next(25)
- 1stData.Items.Add(intRandomNumber.ToString)
- Loop
- End Sub
- Do While...Loop: executes a block of code as long as a condition is true.
- Return to the Forms Designer and add another Button control to your form.
- Set its Name property to btnDoWhileLoop.
- Set Text property to Do While Loop.
- Double-click the button and add the following code to the Click event handler:
- Private Sub btnDoWhileLoop_Click(sender As Object, e As EventArgs) Handles btnDoWhileLoop.Click
- Dim objRandom As New Random
- Dim intRandomNumber As Integer = 0
- ClearList()
- Do While intRandomNumber < 15
- intRandomNumber = objRandom.Next(25)
- lstData.Items.Add(intRandomNumber.ToString)
- Loop
- End Sub
Studying That Suits You
Use AI to generate personalized quizzes and flashcards to suit your learning preferences.