39
Variables and Expressions Programming Right from the Start with Visual Basic .NET 1/e 7

Variables and Expressions Programming Right from the Start with Visual Basic.NET 1/e 7

Embed Size (px)

Citation preview

Variables and Expressions

Programming Right from the Start with Visual Basic .NET 1/e

7

Crews/Murphy – Programming Right from the Start with Visual Basic.NET 1/e – ©2004 Prentice Hall 2

Objectives

• Distinguish between Numeric, String, Char, Boolean, Date, and Object data types

• Explain the similarities and differences between variables and constants

• Understand and use the Dim and Const statements

• Understand and use arithmetic, comparison, and logical operators

Crews/Murphy – Programming Right from the Start with Visual Basic.NET 1/e – ©2004 Prentice Hall 3

Objectives (cont.)

• Explain the advantages of intrinsic functions for performing common calculations

• Understand and effectively use the TextBox, ListBox, and ComboBox controls

• Successfully write Windows applications involving calculations that require complex expressions and intrinsic functions

Crews/Murphy – Programming Right from the Start with Visual Basic.NET 1/e – ©2004 Prentice Hall 4

7-1 Data Types

• A variable is a storage location that can be accessed and changed by developer code.

• A variable has a name and an associated data value.

• A variable has a data type that determines the kind of data values the variable can store.

Crews/Murphy – Programming Right from the Start with Visual Basic.NET 1/e – ©2004 Prentice Hall 5

Numeric Types

• Numeric types have two classes– Integral data types are those that represent

only whole numbers.• Integer is the most common.

– Floating-point data types are those that represent numbers with both integer and fractional parts.

• Double is the most common.

Crews/Murphy – Programming Right from the Start with Visual Basic.NET 1/e – ©2004 Prentice Hall 6

String and Char Types• The Char data type stores a single

character in Unicode format.• The String data type stores a sequence of

Unicode characters.• Unicode is a 16-bit international character

encoding system that covers values for more than 45,000 characters.

• The first 128 characters correspond to the ASCII (American Standard Code for Information Interchange) character set.

Crews/Murphy – Programming Right from the Start with Visual Basic.NET 1/e – ©2004 Prentice Hall 7

Boolean, Data, and Object Types

• The Boolean data type is an unsigned value that is interpreted as either true or false.

• The Date data type stores a date and time value that includes second, minute, hour, day, month, and year values.

• The Object data type is the universal data type in VB. NET.

Crews/Murphy – Programming Right from the Start with Visual Basic.NET 1/e – ©2004 Prentice Hall 8

7-2 The Dim Statement

• Variables are declared with the Dim statement (short for Dimension statement).

• One or more variables can be declared with a single Dim statement.

• The Dim statement can be used to specify the variable’s initial value by placing an equal sign after the data type.

Crews/Murphy – Programming Right from the Start with Visual Basic.NET 1/e – ©2004 Prentice Hall 9

Naming Rules for Variables• A variable must have a unique name, called

an identifier.

• Identifiers must follow three rules:– Begin with a letter or underscore– Contain only letters, digits, and underscores– Cannot be reserved words

Dim identifier [, identifier] [As datatype [= initialvalue]]

Crews/Murphy – Programming Right from the Start with Visual Basic.NET 1/e – ©2004 Prentice Hall 10

Working with Constants

• A constant is a storage location whose value cannot change during the execution of the program.

• Constants are declared with the Const declaration.

Const identifier As datatype = value

Crews/Murphy – Programming Right from the Start with Visual Basic.NET 1/e – ©2004 Prentice Hall 11

7-3 Operators and Expressions

• An operator is a symbol that indicates an action to be performed.

• Value-returning code elements such as variables, constants, and expressions can be combined with one or more operators to form an expression.

Crews/Murphy – Programming Right from the Start with Visual Basic.NET 1/e – ©2004 Prentice Hall 12

Arithmetic andString Operators

• Arithmetic operators require numeric operands and produce numeric results.

• The string operator concatenation (&) requires String operands and produces a String result such as:– S = “sun” & “set”– S becomes “sunset”

Crews/Murphy – Programming Right from the Start with Visual Basic.NET 1/e – ©2004 Prentice Hall 13

Comparison Operators

• Comparison operators require numeric operands and produce a logical result.– Greater than >– Less than <– Greater than or equal >=– Less than or equal <=– Equal =– Not equal <>

Crews/Murphy – Programming Right from the Start with Visual Basic.NET 1/e – ©2004 Prentice Hall 14

Logical Operators

• Logical Operators require logical operands and produce a logical result.– NOT Logical opposite– AND Both values are true– OR At least one value is true– XOR Exactly one value is true

Crews/Murphy – Programming Right from the Start with Visual Basic.NET 1/e – ©2004 Prentice Hall 15

Operator Precedence

• When several operations occur in an expression, each part is evaluated and resolved in a predetermined order called operator precedence.– First: Evaluate all arithmetic/concatenation

operators– Second: Evaluate all comparison operators– Third: Evaluate all logical operators

Crews/Murphy – Programming Right from the Start with Visual Basic.NET 1/e – ©2004 Prentice Hall 16

7-4 The Assignment Statement

• An assignment statement is used to store a value into a variable or an object property.– variable = expression– object.property = expression

• The equal sign (=) is the assignment operator.

Crews/Murphy – Programming Right from the Start with Visual Basic.NET 1/e – ©2004 Prentice Hall 17

7-5 Intrinsic Functions

• VB .NET provides a large number of intrinsic functions to improve developer productivity.

• Intrinsic functions support a broad range of activities, including math calculations, business calculations, time and date functions, string formatting functions, and many more.

Crews/Murphy – Programming Right from the Start with Visual Basic.NET 1/e – ©2004 Prentice Hall 18

Simple Functions

Crews/Murphy – Programming Right from the Start with Visual Basic.NET 1/e – ©2004 Prentice Hall 19

Financial Annuity Functions

• Pmt is a function for determining monthly payments.– Pmt(Rate, NPer, PV)

• FV is a function for determining the future value of an annuity based on periodic, fixed payments and a fixed interest rate.– FV(Rate, NPer, Pmt)

Crews/Murphy – Programming Right from the Start with Visual Basic.NET 1/e – ©2004 Prentice Hall 20

7-6 InputBox and MsgBox

• InputBox and MsgBox are intrinsic functions that facilitate communication with the user by means of a dialog box.

• A dialog box is a pop-up window that informs the user of some information or accepts information from the user.

Crews/Murphy – Programming Right from the Start with Visual Basic.NET 1/e – ©2004 Prentice Hall 21

The InputBox Function

• The InputBox function is a simple means for prompting the user for keyboard input.– Result = InputBox(prompt[, title])

• When executed, InputBox displays a dialog box containing the specified prompt and a text box.

Crews/Murphy – Programming Right from the Start with Visual Basic.NET 1/e – ©2004 Prentice Hall 22

The MsgBox Function

• The MsgBox function displays a message in a dialog box.– MsgBox(prompt)

• The prompt argument is a string expression that gets displayed as the message in the dialog box.

Crews/Murphy – Programming Right from the Start with Visual Basic.NET 1/e – ©2004 Prentice Hall 23

The MsgBox Function (cont.)

Crews/Murphy – Programming Right from the Start with Visual Basic.NET 1/e – ©2004 Prentice Hall 24

7-7 TextBox Control

• The TextBox control provides an area for the user to enter data while the program is executing.

• TextBox controls can be used to display output on the form.

Crews/Murphy – Programming Right from the Start with Visual Basic.NET 1/e – ©2004 Prentice Hall 25

TextBox Properties

Crews/Murphy – Programming Right from the Start with Visual Basic.NET 1/e – ©2004 Prentice Hall 26

TextBox Methods

Crews/Murphy – Programming Right from the Start with Visual Basic.NET 1/e – ©2004 Prentice Hall 27

7-8 ListBox Control

• The ListBox control makes a visible list of items.

• The user can select items in the list using mouse clicks.

Crews/Murphy – Programming Right from the Start with Visual Basic.NET 1/e – ©2004 Prentice Hall 28

ListBox Properties

Crews/Murphy – Programming Right from the Start with Visual Basic.NET 1/e – ©2004 Prentice Hall 29

ListBox Methods

Crews/Murphy – Programming Right from the Start with Visual Basic.NET 1/e – ©2004 Prentice Hall 30

ListBox Illustrated:Select Ice Cream Toppings

• The scroll bar is automatically created by VB. NET if the number of items in the list is too big for the display size of the list box.

• Double-click on an item in the first list box to add the item to the second list box.

• Double-click on an item in the second list box to remove that item.

Crews/Murphy – Programming Right from the Start with Visual Basic.NET 1/e – ©2004 Prentice Hall 31

7-9 ComboBox Control

• The ComboBox control combines the functionality of the TextBox and ListBox controls.

• The user can select values from the drop-down list or enter values directly into the text box area.

Crews/Murphy – Programming Right from the Start with Visual Basic.NET 1/e – ©2004 Prentice Hall 32

ComboBox Properties

Crews/Murphy – Programming Right from the Start with Visual Basic.NET 1/e – ©2004 Prentice Hall 33

ComboBox Illustrated:Movie Voting

• Select a movie from each combo box, then press the vote button.

• The default DropDownStyle property value is DropDown, which makes the text box editable.

• Change the first combo box’s DropDown-Style property to DropDownList and rerun the application.

Crews/Murphy – Programming Right from the Start with Visual Basic.NET 1/e – ©2004 Prentice Hall 34

Chapter Summary

• A variable is a storage location that can be accessed and changed by developer code.

• A constant is a storage location with a name and an associated data value.

• A data type specifies the kind of data that a storage location can hold.

• The unique name for a variable, constant, object, or subroutine is called an identifier.

Crews/Murphy – Programming Right from the Start with Visual Basic.NET 1/e – ©2004 Prentice Hall 35

Chapter Summary (cont.)

• It is good practice to make variable names descriptive with mixed case.

• The Dim statement is used to declare a variable by specifying its name and data type.

• Expressions are combinations of operators and value-returning code elements such as variables, constants, and literals.

Crews/Murphy – Programming Right from the Start with Visual Basic.NET 1/e – ©2004 Prentice Hall 36

Chapter Summary (cont.)

• One of the common uses of an expression is to perform a calculation for an assignment statement.

• The process of changing a value from one data type to another is called conversion.

• Pmt is an intrinsic function that returns the payment for a loan based on periodic, constant payments and a constant interest rate.

Crews/Murphy – Programming Right from the Start with Visual Basic.NET 1/e – ©2004 Prentice Hall 37

Chapter Summary (cont.)

• For a program to be useful, it must perform some type of input/output.

• A TextBox control provides GUI form input by providing an area on the form where the user can enter data while the program is executing.

Crews/Murphy – Programming Right from the Start with Visual Basic.NET 1/e – ©2004 Prentice Hall 38

Chapter Summary (cont.)

• A ListBox control provides GUI form input as a list of items that the user can select by clicking.

• A ComboBox control combines the functionality of the TextBox and ListBox controls.

Variables andExpressions

Programming Right from the Start with Visual Basic .NET 1/e

7