14
VB DATATYPES, VARIABLES, VB DATATYPES, VARIABLES, CONSTANTS & CALCULATIONS CONSTANTS & CALCULATIONS

VB DATATYPES, VARIABLES, CONSTANTS & CALCULATIONS

Embed Size (px)

DESCRIPTION

VB DATATYPES, VARIABLES, CONSTANTS & CALCULATIONS. When a Visual basic program runs, the form and its controls appear on the screen ‘action’. - PowerPoint PPT Presentation

Citation preview

Page 1: VB DATATYPES, VARIABLES, CONSTANTS & CALCULATIONS

VB DATATYPES, VB DATATYPES, VARIABLES, CONSTANTS VARIABLES, CONSTANTS

& CALCULATIONS& CALCULATIONS

Page 2: VB DATATYPES, VARIABLES, CONSTANTS & CALCULATIONS

Visual Basic EventsVisual Basic Events

When a Visual basic program runs, the form and its controls appear on the screen ‘action’.

such as clicking a control clicking a control or pressing a keypressing a key. We call such an action an eventan event. The programmer writes code that reacts to an event by performing some functionality.

The three steps three steps in creating a VB.NET program :

Page 3: VB DATATYPES, VARIABLES, CONSTANTS & CALCULATIONS

Visual Basic EventsVisual Basic Events

Create the interfaceCreate the interface generate, position, and size the objects.

Set propertiesSet propertiesconfigure the appearance of the objects.

Write the codeWrite the code executes when events occur.

This is called Event-driven programming Event-driven programming where the programs allow responding to where the programs allow responding to many different input or events.many different input or events.

Page 4: VB DATATYPES, VARIABLES, CONSTANTS & CALCULATIONS

VISUAL BASIC.NET DATA TYPESVISUAL BASIC.NET DATA TYPES

Data type Data type is a class that is primarily used just to hold type of data of variables…

 

Page 5: VB DATATYPES, VARIABLES, CONSTANTS & CALCULATIONS

.NET Data types

Data TypeSize in Bytes

DescriptionType

Byte18-bit unsigned integerSystem.Byte

Char216-bit Unicode characters

System.Char

Integer432-bit signed integerSystem.Int32

Double864-bit floating point variable

System.Double

Long864-bit signed integerSystem.Int64

Short216-bit signed integerSystem.Int16

Single432-bit floating point variable

System.Single

StringVariesNon-Numeric TypeSystem.String

Date8System.Date

Boolean2Non-Numeric TypeSystem.Boolean

Object4Non-Numeric TypeSystem.Object

Decimal16128-bit floating point variable

System.Decimal

Page 6: VB DATATYPES, VARIABLES, CONSTANTS & CALCULATIONS

Properties in VB controls also have data types, and when we assign values to the properties, those values must be of proper data type.

Think back to some of the properties we have been using -- each has its own data type:

PropertyPropertyData TypeData Type

namestring

Textstring

visibleboolean

multilineboolean

Page 7: VB DATATYPES, VARIABLES, CONSTANTS & CALCULATIONS

VARIABLESVARIABLES

Used to storestore data in memory., and later retrieve them.

Like the properties of objects, variables have names, data types, and valuesnames, data types, and values. As the name implies, the values of variables can change (vary) during the execution of the program.

Page 8: VB DATATYPES, VARIABLES, CONSTANTS & CALCULATIONS

The name, data type and an optional initial value of each variable is specified with a DimDim statement. Dim stands for Dimension.

Dim <variable name> as <data type> Dim <variable name> as <data type> [=expiration] [=expiration]

when the Dim statement is executed the compiler sets aside space in memory to hold it's value. (The amount of memory it needs to set aside depends upon the data type).

Page 9: VB DATATYPES, VARIABLES, CONSTANTS & CALCULATIONS

For exampleFor example: : consider the following Dim statements:

Dim strName As String = "Bob" Dim intAge As Integer = 21 Dim sglPay As Single = 7.50

When the computer executes these instructions, it would construct three variables, setting aside space in memory for each and assigning it the appropriate initial values

Page 10: VB DATATYPES, VARIABLES, CONSTANTS & CALCULATIONS
Page 11: VB DATATYPES, VARIABLES, CONSTANTS & CALCULATIONS

We made up variable names that indicate the data type (str, int, sgl) and the meaning (name, age, pay rate).

A variable name can be anything you wish, but words that have specific meaning in Visual Basic, like "Dim" or "CInt," are reserved words (or Keywords) reserved words (or Keywords) and cannot be used as variable names.

Note:The syntax of the Dim statement indicates that

expression giving the initial value is optional. If it is not specified, numeric variables are assigned an

initial value of zero (0) and string variables are assigned an initial value of Null ("").

Page 12: VB DATATYPES, VARIABLES, CONSTANTS & CALCULATIONS

Examples:Examples: Initialization !!Initialization !!Numeric variables are automatically initialized to 0:

Dim varName As Double  

To specify a nonzero initial value

Dim varName As Double = 50  OrDim varName As DoubleVarName = 50  

Page 13: VB DATATYPES, VARIABLES, CONSTANTS & CALCULATIONS

Cont .. Examples:Cont .. Examples: Incrementing !!Incrementing !!To add 1 to the numeric variable var

var = var + 1  Or as a shortcut

var +=1  

Page 14: VB DATATYPES, VARIABLES, CONSTANTS & CALCULATIONS

copycopy

Cont .. Examples:Cont .. Examples: Integer Data Type !!Integer Data Type !!An integer is a whole number ..Declaring an integer variable:

Dim varName As Integer  Multiple Declarations

Dim a, b As Double  Two other types of multiple-declaration statements areDim a As Double, b As IntegerDim c As Double = 2, b As Integer = 5