Title: Introduction to Numeric Data Types and Variables
1Chapter 3
- Introduction to Numeric Data Types and Variables
2Objectives (1)
- Create a text box for input and output and define
an accept and cancel button for a form - Define the order in which the user navigates from
one control instance to another by pressing the
Tab key - Explicitly set input focus and create ToolTips
for control instances - Create variables to store information while a
solution runs - Understand the organization of namespaces
3Objectives (2)
- Learn how to create comments
- Understand the events that occur as a user
navigates between text boxes - Perform computations with variables using
assignment statements - Understand the role of precedence, perform type
conversions, and format data - Write a statement on multiple lines and calculate
solution output
4The Text Box Control
- Used to display output to the user and get text
input from the user - Similar to a Label control but a Label control
only displays output - Standard prefix for a text box is "txt"
- User enters text into one text box at a time
- This text box is said to have input focus or focus
5The TextBox Control Properties (1)
- BorderStyle - defines the border surrounding the
text box - ForeColor, BackColor - defines the text color and
background color respectively - Enabled - controls whether the text box can get
focus and will respond to events - Locked - controls whether user can change the
text appearing in the text box - MultiLine - controls whether text will appear on
multiple lines - MaxLength - defines the maximum number of
characters that the user can enter into the text
box
6The TextBox Control Properties (2)
- Name - defines name used to reference control
instance with code - Again, use a standard prefix of "txt"
- ScrollBars - defines whether vertical or
horizontal scroll bars appear in text box - Text - contains the text appearing in text box
- TextAlign - justifies text within the visible
region of the control instance - X, Y, Height, Width properties define the
position and size of the control instance
7The TextBox Control Methods and Events
- Focus method sets input focus to a text box
- Enter event fires when text box gets focus
- Leave event fires when text box loses focus
- TextChanged event fires when the changes the
textual contents at run time - The event fires for each character typed
8The AcceptButton and CancelButton Properties
- One button on a form can be designated as the
accept button and another the cancel button - Set the forms AcceptButton property to a Button
control instance to define the accept button - Click event handler executes when user presses
the Enter key - Set the forms CancelButton property to Button
control instance to define the cancel button - Click event handler executes when user presses
the Esc key
9Setting the Tab Order (1)
- Order in which control instances get focus is
called the tab order - The TabIndex property defines tab order
- Set the TabIndex property to a unique Integer
value for each control instance that can get focus
10Setting the Tab Order (2)
- To set the tab order visually
- Activate the Win Form Designer
- On the menu bar, click View, and then click Tab
Order - Click the control instances in the order that you
want them to get focus - Click View, and then click Tab Order again to
record your changes
11Setting the Tab Order (3)
Tab order of control instances appear in
upper-left corner of control instance region
12Explicitly Setting Input Focus
- Call the Focus method of a text box or other
object that can get input focus - Set the input focus to the text box named
txtInitialValue - txtInitialValue.Focus()
13ToolTips
- ToolTips appear as a pop-up when the mouse hovers
over a control instance for a short period - Standard prefix is "tip"
- InitialDelay property defines the time interval
for mouse to hover before the ToolTip appears - AutomaticDelay property defines the amount of
time that the ToolTip appears - After creating the ToolTip, set the ToolTip on
control property for each control instance - Note that the control instance appears in
resizable tray below the form because it is not
visible at run time
14ToolTips (Example)
ToolTip on TipGeneral Property
ToolTip control instance
15Storing Data in Variables
- Variable stores data while a solution runs
- A variable is simply a unique name that
identifies a space in memory (RAM) - Data may be a string of characters or a number
- Every variable has a data type
- A variable's data type determines the kind of
information that the variable can store - Numbers, characters, etc.
- Every variable has a name
- Use standard prefixes for variable names to
denote data type
16Selected VB .NET Data Types (1)
17Selected VB .NET Data Types (2)
18Standards for Variable Names (1)
- Use a one-character prefix to denote scope
- The scope of a variable indicates which event
handlers can use that variable - Use a three-character prefix to denote data type
- Add an additional prefix character to denote a
variable's scope - Module-level variables carry a prefix of "m"
19Standards for Variable Names (1)
20Declaring a Variable (1)
- Private statement declares variable
- Private varname As type initexpr
- varname is variable name
- As type contains data type of variable
- Optional initexpr contains the initial value of a
variable - Declare variables having the Double and Integer
data types - Private mdblInterestRate As Double
- Private mintCounter As Integer
21Declaring a Variable (2)
- Initialize a variable when declaring it
- New to VB .NET
- Declare an Integer variable and store the value
30 in the variable - Private mintYearTerm As Integer 30
22Declaring a Variable (3)
- Do not include punctuation characters in a
variable declaration - The following declarations are illegal because of
the sign and commas - Private mdblInitialValue As Double 100,000.52
- Private mdblInitialValue As Double 100,000.52
- Possible to declare multiple variables on the
same line - Private mintYearTerm, mintMonthTerm As Integer
23Declaring Module-Level Variables
Declare module-level variables after the
statements generated by the Win Form Designer but
outside an event handler
24The Organization of Namespaces (1)
- VB .NET accesses predefined components via
namespaces - Namespaces developed by .NET team begin with
System - Namespaces developed by others at Microsoft being
with Microsoft - Namespaces are organized hierarchically
25The Organization of Namespaces (2)
System namespace System
Drawing namespace System.Drawing
Windows.Forms namespace System.Windows.Forms
Data namespace System.Data
Printing namespace System.Drawing.Printing
26Referencing Namespaces
- Use dot syntax to connect namespaces together
- A period separates each namespace name
- System.Drawing
- System.Windows.Forms
- System.Data
27Parts of a Namespace (1)
- A namespace contains
- Enumerations which define a symbolic name for a
value - Classes which are reference types
- Structures which are value types
28Parts of a Namespace (2)
Windows.Forms namespace System.Windows.Forms
Classes
Structures
Enumerations
Button
BorderStyle
Message
Label
FormBorderStyle
TextBox
29Namespace Enumerations
- Enumerations define a symbolic name for a value
- Enumerations improve code readability
- The BorderStyle enumeration applies to the
System.Windows.Forms namespace - lblFileName.BorderStyle _ System.Windows.Form
s.BorderStyle.FixedSingle
30Namespace Classes
- Classes are reference types
- Variable stores the memory address of an object
rather than the object itself - Controls appearing in the Toolbox are all classes
Variable btnCalculate 023448
Variable contains memory address of Button
control instance
btnCalculate (Memory address 023448) Data
Caption "Calculate"
31Namespace Structures (1)
- Structures are value types
- Store data directly in the variable
- Primary data types such as Boolean Integer,
Single, and Double are all structures - Note that the String data type is a class though
32Namespace Structures (2)
Private mcolCurrent As System.Drawing.Color
mcolCurrent System.Drawing.Color.Navy
Allocate memory for the variable
Store the value Navy directly in the variable
variable mcolCurrent Navy
33Object Browser (1)
- Use the Object Browser to identify the properties
and methods supported by classes - Object Browser does not create code for you
- Think of it as a roadmap to the namespaces,
classes, structures, and enumerations defined by
VB .NET
34Object Browser (2)
35Creating Well-Written Comments (1)
- A Comment is a full or partial line of text
ignored by VB .NET - Create comments to describe what your code is
doing or how code performs a particular task - Multiple comments are called a comment block
- Blank lines between statements are ignored and
are called whitespace - A comment can appear on its own line and begins
with a single quote ( ' ) - A comment can appear at the end of a line
36Creating Well-Written Comments (2)
- The following is a comment appearing on its own
line - ' This line is a comment.
- The following comment appears at the end of a
line. Note a space must precede the comment
character - txtDemo.text "Dog" ' Store "Dog" in the text
box
Comment
37Creating Well-Written Comments (3)
VB .NET displays comments in in green
38Writing Assignment Statements Using Variables
- Store a property in a variable
- mintResult txtExample.Height
- Store a literal value in a variable
- mintResult 3
- Store another variable in a variable
- mintResult mintExample
39Arithmetic Operators (1)
- Expressions consists of literal values,
constants, variables and arithmetic operators - An arithmetic operator performs a mathematical
calculation - Multiply the literal values 3 and 2 together and
store the result (6) in the variable mintResult - mintResult 3 2
40Arithmetic Operators (2)
41Arithmetic Operators Example
- Divide two numbers and multiply the intermediate
result by 100 - Private mdblNumerator As Double 10
- Private mdblDenominator As Double 5
- Private mdblResult As Double
- mdblResult mdblNumerator / mdblDenominator 100
42The Role of Precedence in Expressions
- Programming languages evaluate arithmetic
expressions from left to right in a predefined
order known as precedence - Perform exponentiation first
- Next perform multiplication and division
- Next perform Integer division and then apply the
Mod operator - Next perform addition and subtraction
- Parenthesis override default precedence
- Parenthesis may be nested
43Precedence (Example)
(Var1 Var2) / (Var3 Var4) (Var5 Var6)
1 (addition)
2 (addition)
3 (exponent)
4 (division)
5 (multiplication)
44Type Conversion
- VB .NET must convert data from one data type to
another when evaluating mathematical expressions - The Option Strict On statement enables strict
type checking - Strict type checking requires explicit type
conversion from less restrictive types to more
restrictive types - Use strict type checking to minimize hard to find
errors resulting from implicit type conversion
45Strict Type Checking (Example)
- The following statement will cause an error if
strict type checking is enabled. If disabled,
mintPI will store the value 3 - Note that the result if the implicit type
conversion is truncation - Private mintPI As Integer 3.14159
46Option Explicit
- Option Explicit On statement requires that you
declare variables before using them the first
time - Also reduces hard to find errors resulting from
typos - Note that Option Explicit and Option Strict
statements appear at the beginning of a module
47Type Conversion Methods
- Methods belong to the System.Convert class
- ToInt16 converts value to a Short
- ToInt32 converts value to an Integer
- ToInt64 converts value to a Long
- ToDouble converts value to a Double
- ToSingle converts value to a Single
- ToString converts value to a String
- Each method takes one argument the value to
convert - Note that these methods supercede older intrinsic
functions like CInt and CLng
48Type Conversion Examples
- Private msngInput As Single 3.44
- Private mstrInput As String "3.95"
- Private mintOutput As Integer
- Private msngOutput As Single
- Convert a Single to an Integer
- mintOutput System.Convert.ToInt32(msngInput)
- Convert a String to an Integer
- mintOutput System.Convert.ToInt32(mstrInput)
- Convert a String to a Single
- msngOutput System.Convert.ToSingle(mstrInput)
49Numeric Overflow (1)
- Numeric overflow occurs when trying to store too
large a number in a variable - The following statements will cause a numeric
overflow error because the largest number that
can be stored in a short is 32767 - Private mshoExample As Short
- mshoExample 46000
- mshoExample 2500 2500
50Numeric Overflow (2)
- VB .NET performs arithmetic using less
restrictive type on right side of expression - The last statement will cause numeric overflow
- Private mshoArg1 As Short, mshoArg2 As Short
- Private mintArg3 As Integer
- mshoArg1 32000
- mshoArg2 32000
- mintArg3 mshoArg1 mshoArg2
51Formatting a Numeric Value with Methods
- The ToString type conversion method accepts a
string argument to format data - Special characters are used to define the format
- lblGain.Text mdblGain.ToString(",.")
variable
ToString method
argument defines how to format the value
52Formatting Placeholders
53Formatting Examples
54Continuation Lines
- Use when statement will not fit on a line
- Underscore (_) is the continuation character
- Rules for use
- Space must precede continuation the character
- Comment following continuation character is not
allowed - Do not break up words
- Multiple continuation lines allowed
55Continuation Lines (Example)
- The following statement appears on two lines
- mdblInterestRate _
- System.Convert.ToDouble(txtInterestRate.Text) /
100
56The Intrinsic FV Function
- Calculate the future value of a fixed amount of
money based on a constant interest rate - Syntax
- result FV( rate, periods, payment ,
presentvalue , type ) - Rate is interest rate
- Periods is number of time periods
- Regular payments, if made stored in payment
- Value at start of term stored in present value
- Type indicates when payments are made
57FV Function - Example
- Calculate the future value of 1000.00 for 12
periods at a rate of 1 per period - mdblFutureValue FV(.01, 12, 0, 1000)
58The System.Math Class
- Performs common mathematical operations
- Static methods Do not create an instance of the
class - Abs method calculates absolute value
- Sqrt method calculates square root of a number
- Sin, Cos, and Tan compute sine, cosine and
tangent of a number - Many others exist