Introduction to Tools, Numeric Data Types and Variables

Slides:



Advertisements
Similar presentations
Lecture Set 4 Data Types and Variables Part B – Variables, Constants, Expressions Conversion Rules Options Strict, Option Explicit Scope of Definition.
Advertisements

Objectives Understand the software development lifecycle Perform calculations Use decision structures Perform data validation Use logical operators Use.
Chapter 3.3 Numeric Data Types and Variables. Slide 2 Objectives Create variables to store information while a solution runs Perform computations with.
IS437: Fall 2004 Instructor: Dr. Boris Jukic Data Types, Constants, Variables, Scope, Conversion.
Introduction to C Programming
Data Types 1.
Chapter 3 Introduction to Event Handling and Windows Forms Applications.
Visual Basic Chapter 1 Mr. Wangler.
Chapter 4 Introduction to Numeric Data Types and Variables.
Variables and Constants
© 2006 Lawrenceville Press Slide 1 Chapter 3 Visual Basic Interface.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Visual Basic.NET Comprehensive Concepts and Techniques Chapter 4 Working with Variables, Constants, Data Types, and Expressions.
Input, Output, and Processing
 Application – another name for a program.  Interface – is what appears on the screen when the application is running.  Program Code – is instructions.
Chapter 2: Using Data.
Representing Data: Constants and Variables CHAPTER THREE Matakuliah: T0063 – Pemrograman Visual Tahun: 2009.
Microsoft Visual Basic 2005 CHAPTER 4 Variables and Arithmetic Operations.
Chapter 4 Variables and constants. 4.1 Variables -Use of variables is good programming style -easier to modify -easier for a programmer to understand.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 3 Variables, Constants, Methods, and Calculations.
Variables & Function Calls. Overview u Variables  Programmer Defined & Intrinsic  Data Types  Calculation issues u Using Functions  The val() function.
Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Three Memory Locations and Calculations.
Chapter 4 Variables and constants. 4.1 Variables -Use of variables is good programming style -easier to modify -easier for a programmer to understand.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 2A Reading, Processing and Displaying Data (Concepts)
CHAPTER THREE Representing Data: Constants and Variables.
Variables and Expressions Programming Right from the Start with Visual Basic.NET 1/e 7.
Microsoft Visual Basic 2012 CHAPTER FOUR Variables and Arithmetic Operations.
Visual Basic.NET Programming for the Rest of Us Keith Mulbery Utah Valley State College.
Making Interactive Programs with Visual Basic .NET
Chapter 4.  Variables – named memory location that stores a value.  Variables allows the use of meaningful names which makes the code easier to read.
Slide 1 Chapter 3 Variables  A variable is a name for a value stored in memory.  Variables are used in programs so that values can be represented with.
© 2006 Lawrenceville Press Slide 1 Chapter 4 Variables  A variable is a name for a value stored in memory.  Variables are created using a declaration.
Data Types. Visual Basic provides data type Single for storing single-precision floating-point numbers. Data type Double requires more memory to store.
Representing Data: Constants and Variables
Microsoft Visual Basic 2010 CHAPTER FOUR Variables and Arithmetic Operations.
IS 350 Numeric Data Types.
Visual Basic.NET Windows Programming
Egyptian Language School Computer Department
A variable is a name for a value stored in memory.
Chapter 2 Variables.
Topics Designing a Program Input, Processing, and Output
Arithmetic operations and operators, converting data types and formatting programs for output. Year 11 Information Technology.
Chapter 6 JavaScript: Introduction to Scripting
IS 350 Course Introduction
Variables and Arithmetic Operations
Chapter 3: Using Variables and Constants
Variables, Expressions, and IO
How to design a Windows Forms application
Lecture Set 4 Data Types and Variables
Data Types and Variables Part D – Common Methods (ToString and Parse)
Variables and Arithmetic Operations
Introduction to C++ Programming
WEB PROGRAMMING JavaScript.
Visual Basic Programming Chapter Four Notes Working with Variables, Constants, Data Types, and Expressions GROUPBOX CONTROL The _____________________________________.
CIS16 Application Development Programming with Visual Basic
Chapter 2 Variables.
VB.NET PROGRAMMING FINAL EXAM TEST REVIEW.
Visual Basic Numbers Chapter 3.3 Prepared By: Deborah 1/15/2019.
Chapter 3 – Introduction to C# Programming
Topics Designing a Program Input, Processing, and Output
Computing in COBOL: The Arithmetic Verbs and Intrinsic Functions
Chapter 2: Introduction to C++.
Topics Designing a Program Input, Processing, and Output
Introduction to Programming
Topics Designing a Program Input, Processing, and Output
Chapter 2 Variables.
Visual Basic Numbers Chapter 3.3 Prepared By: Deborah 7/9/2019.
PYTHON - VARIABLES AND OPERATORS
Presentation transcript:

Introduction to Tools, Numeric Data Types and Variables Chapter 3 Introduction to Tools, Numeric Data Types and Variables

Objectives 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

Objectives 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

The 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

TextBox Control – Properties 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

TextBox Control – Properties 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

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

AcceptButton & CancelButton Properties One button on a form can be designated as the accept button and another the cancel button Set the form’s AcceptButton property to a Button control instance to define the accept button Click event handler executes when user presses the Enter key Set the form’s CancelButton property to Button control instance to define the cancel button Click event handler executes when user presses the Esc key

Setting the Tab Order 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

Setting the Tab Order 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

Setting the Tab Order Tab order of control instances appear in upper-left corner of control instance region

Explicitly 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()

ToolTips 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

ToolTips (Example) ToolTip on TipGeneral Property ToolTip control instance

Storing 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

Numeric VB .NET Data Types Storage Size Prefix Possible Values Boolean 2 bytes bln True or False Date 8 bytes dat Dates between 1/1/0001 and 12/31/9999 Short sho Positive and negative whole numbers between –32768 and 32767 Integer 4 bytes int Positive and negative whole numbers between –2,147,483,648 and 2,147,483,647 Long lng Positive and negative whole numbers

Numeric VB .NET Data Types Storage Size Prefix Possible Values Single 4 bytes sng A number with at most 6 digits to the right of the decimal point Double 8 bytes dbl A number with at most 14 digits to the right of the decimal point String 1 byte per character str Character string of up to about 2 billion characters

Standards for Variable Names 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"

Standards for Variable Names Valid Variable Name Invalid Variable Name Cause mlngCounter mlng.Counter A period (.) mintTotal mint Total A space mdblFutureValue mdlbFuture-Value A dash (-) mdblGain mdbl*Gain Special characters

Declaring a Variable Private statement declares variable Private varname As type [ = initialization expression] varname is variable name As type contains data type of variable Optional initialization expression contains the initial value of a variable Example: Declare variables having the Double and Integer data types Private mdblInterestRate As Double Private mintCounter As Integer

Declaring a Variable Initialize a variable when declaring it New to VB .NET Example: Declare a modular Integer variable and store the value 30 in the variable Private mintYearTerm As Integer = 30 Focus (m) Prefix (int) Name YearTerm

Declaring a Variable Do not include punctuation characters in a variable initialization 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

Declaring Module-Level Variables Declare module-level variables after the statements generated by the Window Form Designer but outside an event handler

What is a namespace? Namespaces are a way to define the classes and other types of information into one hierarchical structure. System is the basic namespace used by every .NET code. If we can explore the System namespace little bit, we can see it has lot of namespace using the system namespace. For example, System.Io, System.Net, System.Collections, System.Threading, etc.

The Organization of Namespaces VB .NET accesses predefined components via namespaces Namespaces developed by .NET teams begin with System Namespaces developed by others at Microsoft begin with Microsoft Namespaces are organized hierarchically

The Organization of Namespaces System namespace System Drawing namespace System.Drawing Windows.Forms namespace System.Windows.Forms Data namespace System.Data Printing namespace System.Drawing.Printing

Parts of a Namespace A namespace contains: Enumerations which define a symbolic name for a value Classes which are reference types Structures which are value types

Windows.Forms namespace System.Windows.Forms Parts of a Namespace Windows.Forms namespace System.Windows.Forms Classes Structures Enumerations Button Message BorderStyle Label FormBorderStyle TextBox

Namespace 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.Forms.BorderStyle.FixedSingle Namespace Property Enumeration

Namespace 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 (name) (Memory address 023448) Data btnCalculate.text = "Calculate" …

Namespace Structures 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 We will discuss strings separately

Creating Well-Written Comments 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

Creating Well-Written Comments 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

Creating Well-Written Comments VB .NET displays comments in in green

Writing 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

This is also called an assignment statement Arithmetic Operators 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 This is also called an assignment statement

Arithmetic Operators (2) Operators in Order of Precedence Description Example ^ Raises a number to the power of an exponent 2 ^ 3 is equal to 8 * / Multiplication and Division 2 * 3 is equal to 6 8 / 4 is equal to 2 \ Integer Division 10 \ 3 is equal to 3 5 \ 2 is equal to 2 Mod Modulus arithmetic 10 Mod 3 is equal to 1 + - Addition and subtraction 2 + 3 is equal to 5

Arithmetic 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

The Role of Precedence 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

(Var1 + Var2) / (Var3 + Var4) * (Var5 ^ Var6) Precedence (Example) (Var1 + Var2) / (Var3 + Var4) * (Var5 ^ Var6) 1 (addition) 2 (addition) 3 (exponent) 4 (division) 5 (multiplication)

Type 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

Strict 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

Option 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

Type 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

Type Conversion Examples Private msngInput As Single = 3.44 Private mstrInput As String = "3.95“ ‘ why use “” 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)

Numeric Overflow 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

Numeric Overflow 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

Formatting 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

Formatting Placeholders Character Description Digit placeholder. If number corresponding to position does not contain a value, then a 0 appears # Digit placeholder. If number corresponding to position does not contain a value, then a space appears . Decimal placeholder. Defines the position of the decimal point , Thousands separator % Percentage placeholder - + $ ( ) Literal characters embedded in the format string

Formatting Examples Format String (Mask) Value Formatted value 000000.000 123.45 000123.450 ###,###.00 1234.4 1,234.40 $###,###.00 $1,234.40 ###% 0.08 8% ##.00% 8.00% C $1234.40 F 1234.40

Continuation Lines Use when statement will not fit on a line Underscore (_) is the continuation character Rules for use A Space (⇑) must precede the continuation character You cannot follow a continuation character with a Comment Do not break up words Multiple continuation lines allowed

Continuation Lines (Example) The following statement appears on two lines: mdblInterestRate =⇑_ System.Convert.ToDouble(txtInterestRate.Text) / 100