Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Variables and Expressions Programming Right from the Start with Visual Basic.NET 1/e 7."— Presentation transcript:

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

2 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

3 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

4 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.

5 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.

6 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.

7 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.

8 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.

9 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]]

10 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

11 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.

12 Crews/Murphy – Programming Right from the Start with Visual Basic.NET 1/e – ©2004 Prentice Hall 12 Arithmetic and String 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”

13 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 <>

14 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. –NOTLogical opposite –ANDBoth values are true –ORAt least one value is true –XORExactly one value is true

15 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

16 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.

17 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.

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

19 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)

20 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.

21 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.

22 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.

23 Crews/Murphy – Programming Right from the Start with Visual Basic.NET 1/e – ©2004 Prentice Hall 23 The MsgBox Function (cont.)

24 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.

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

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

27 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.

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

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

30 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.

31 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.

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

33 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.

34 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.

35 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.

36 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.

37 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.

38 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.

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


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

Similar presentations


Ads by Google