200 likes | 335 Vues
This chapter delves into the fundamentals of using variables, constants, and functions within Visual Basic .NET for efficient calculations. It explains the various data types including strings, bytes, and characters, along with how to declare and manage variables. Learn about accessibility, default values, and the importance of explicit variable declaration to minimize errors. The chapter also covers local and module-level variables, as well as the use of functions and arithmetic operators to perform calculations. Master these concepts to enhance your programming skills!
E N D
Using Variables, Constants, and Functions in Calculations Chapter 2
Text Data Types • String stores up to 2 billion alphanumeric characters – letters, digits, and special characters. • Byte stores 1 ASCII character. • Char stores 1 unicode character – includes 65,000 characters consisting of many languages.
Declaring Variables • You must declare or create a variable before you can use it. Accessibility VariableName As Type [= Initial Value] • Accessibility defines what procedures have access to the value stored in the variable. • Dim or Private • Initial Value is optional.
Default Values • Numeric – 0 • Boolean – False (-1) • Date – 12:00 AM January 1, 0001 • Object – null • String - null
Variable Declaration • Explicit means you must use the Dim statement before you can use the variable. • Implicit means you can use the variable without a Dim statement. It will default to the Object data type. • You must use Option Explicit Off statement for implicit declaration. • Explicit variable declaration is one method of minimizing errors in your application.
Local Variable • Accessibility specifies which program statements will have access to the value stored in the variable. • Accessibility is also called variable scope. • A variable is a local variable and has procedure scope if it is declared within a procedure. • The Dim statement declares a local variable.
Module Level Variable • A module (or form) level variable is accessible to any procedure within a form. • A variable is a module level variable and has module scope if it is declared in the Form Declarations section. • The Private statement declares a module level variable.
Declaring Constants • Local Constant defined in procedure Const conName As datatype = expression • Module (form) level constant defined in General Declarations section Private Const conName As datatype = expression
Using ComboBox values • Dim intTerm as Integer • intTerm = Val(cboTerm.Text)
A function is a procedure that returns a value. Arguments are values supplied to the function. Functions are used to simplify calculations. Variable = Val(argument) Val Format InputBox MsgBox IPmt Pmt Ppmt Implicit (Predefined) Functions
InputBox Function Variable = InputBox(Prompt, [Title], [Default Response])
Arithmetic Operators • ^ Exponentiation • * Multiplication • / Division with floating point result • \ Division with an integer result • MOD – Result is the remainder of division • + Addition • - Subtraction
Format Function • Format (expression, “style”) • Style is either a: pre-defined format ie. currency or User defined format ie. $#,###.00