Download presentation
Presentation is loading. Please wait.
Published byMartina April Holmes Modified over 8 years ago
1
Visual Basic.NET Comprehensive Concepts and Techniques Chapter 5 Decision Making
2
2 Modifying the Automobile Loan Calculator Application
3
3
4
4 New Control – ComboBox ComboBox control – used in Windows applications to present lists of choices. Uses the prefix cmb. DropDownStyle Property – used to change the appearance of the list and the method used to select an item. –Simple: List always displays; scroll bar added if list is longer than the ComboBox height. –DropDown: List drops down when you click on the box. You can type directly into the box. (Default) –DropDownList: List drops down when you click on the box; you CANNOT type directly into the box. Items Property – used to build the list!
5
5 Adding Items to the ComboBox During Design Time
6
6 Understanding ComboBox Indexes When you add items to a ComboBox List VB.Net assigns each one an unique index number. The first item is assigned an index of 0; the second item gets an index of 1; and so on. –For example: Index Numbers for Auto Loan Program A -- Excellent Credit Rating = 0 B – Good Credit Rating = 1 etc …. SelectedIndex is the property that keeps track of what index number is currently being accessed. When no item is selected, the SelectedIndex property is set to -1. The following line of code would clear the ComboBox:
7
7 Decision-Making Control Structures In previous chapters, the programs were designed to perform precisely the same computations for every set of data items that were processed. In some instances, you want it to do the calculation different ways under different circumstances. –For example: The Auto Loan Program uses higher interest rates for those deemed to be “higher risk” loans. –In order to do this the program must be coded to make a decision concerning which interest rate to use! Decision-making is the process of determining which of one or more paths to take. We will learn two new code structures to help you begin this process! –If…Then…Else structure –Select Case structure
8
8 If…Then…Else Structure Select Case Structure
9
9 If…Then…Else Structure for Auto Loan Calculator
10
10 IF … THEN … ELSE STATEMENTS If…Then…Else statements are used to execute one statement or another conditionally. –The condition follows the keyword IF –A condition is made up of two expressions and a relational (comparison) operator. Equal =Less than or equal to <= Less than = Greater than > Not equal to <> Nested If.. Then … Else Structure: when you have an if…then within an if then!
11
11 MORE ABOUT IF.. THEN.. ELSE The statement to be executed if the condition is true follows the keyword then. The statement to be executed if the condition is false follows the keyword else. For Example: If dblSales > 500 then dblBonus = 100 Else dblBonus = 0 End if If a variable is declared within a block if..then, it will only work in that block. That is referred to as block-level scoping.
12
12 Another Example of If … Then As we mentioned earlier … you can have a nested if … then structure: which is an if then within another if … then. For example: If radTwoYears.Checked = True Then gdblMonths = 24 Else If radFiveYears.Checked = True Then gdblMonths = 60 Else gdblMonths = 72 End if Note: Every If needs an End if!
13
13 Select Case Structure Used to implement an extension of the If…Then…Else structure. Select Case Structure: used when there are more than two options! This was perfectly illustrated in the Auto Loan Program. They used a select case structure to input the correct interest rates for the five possible credit ratings!
14
14 Coding a Select Case Statement
15
15 Coding a Select Case Statement
16
16 Using Message Boxes Sometimes it becomes important to communicate with the person running your program. –For example: They do something stupid and you want to tell them not to do that anymore! The MessageBox provides the ability to display a message to the user in a pop-up window. The Show() method can be used to display a message with a title bar. Basically, it consists of three parts! For example: MessageBox.Show(“Please enter the customer’s credit rating in the Credit rating list box.”,”No Credit Rating”)
17
17 Logical Operators The And Logical Operator – is used to combine two or more conditions in an expression. All parts must be true for the True section of code to trigger! For example: If strGender = “M” And intAge < 12 Then MessageBox.Show(“Boys Little League”) End if
18
18 Logical Operators – Part Two The Or Logical Operator – also used to combine two or more expressions! However, the or operator only requires one condition to be true for the true condition code to trigger! For example: If strGrade = “E” Or intAbsences > 10 then strCredit = “No Credit” End if
19
19 Combining Logical Operators If X>Y Or T=D And H<3 Or Y=R Then intCount = intCount + 1 End If What order would this line of code execute in? To understand how.NET evaluates this code you’d have to understand the “Rules of Precedence” Unless parenthesis dictate otherwise,.NET reads from left to right and evaluates conditions in this order: –Arithmetic operators –Relational operators –And operators –Or operators
20
20 Coding with a Logical Operator and String Expressions
21
21 CONCATENATING TEXT Concatenation -- the process of adding strings together. It is performed with the ampersand [ &] character. –For example: strErrorMessage = “Customers with this credit rating may only borrow “ & strMoney The plus sign (+) can be used instead of the (&) operator. Often times programmers avoid the + sign since it is used in mathematical problems as well.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.