types of visual basic data numeric data non numeric data

38
Types of Visual Basic Data Numeric Data Non Numeric Data

Upload: chad-alexander

Post on 17-Jan-2016

239 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Types of Visual Basic Data Numeric Data Non Numeric Data

Types of Visual Basic Data

•Numeric Data

•Non Numeric Data

Page 2: Types of Visual Basic Data Numeric Data Non Numeric Data

Type Storage (Bytes)

Type of Values

Byte 1 Whole numbers

Integer 2 Whole Numbers

Long 4 Whole Numbers

Single 4 Decimal Numbers

Double 8 Decimal Numbers

Currency 8 Numbers with 4 digits to right of decimal

Numeric Data

Page 3: Types of Visual Basic Data Numeric Data Non Numeric Data

Non Numeric Data Datatype Storage(Bytes)

String(Fixed length) 65000 characters String(length+10Bytes) 2 billion characters

Date 8

Boolean 2

Object 4 Variant(numeric) 16

Variant(text) Length+22

Page 4: Types of Visual Basic Data Numeric Data Non Numeric Data

Variables• Variables are areas allocated by the computer

memory to hold data.

• Each variable must be given a name.

• Rules : It must be less than 255 characters

No spacing is allowed

It must not begin with a number

Period (.) is not permitted

Valid ones:txt123,My_Car Invalid : 123txt,My.car

Page 5: Types of Visual Basic Data Numeric Data Non Numeric Data

Declaring Variables

In Visual Basic, one needs to declare the variables before using them by assigning names and data types.

They are normally declared in the genaral section of the codes' windows using the Dim statement.

Dim variableName as DataType

Example :

Dim password As String Dim yourName As String Dim firstnum As Integer Dim secondnum As Integer Dim total As Integer Dim doDate As Date

Dim password As String, yourName As String, firstnum As Integer,.............

Page 6: Types of Visual Basic Data Numeric Data Non Numeric Data

If data type is not specified, VB will automatically declares the variable as a Variant. For string declaration,Two ways of declaration-

•variable-length string-

Dim a as String

•fixed-length string.

Dim VariableName as String * n, where n defines the number of characters the string can hold.

Example:

Dim yourName as String * 10

yourName can holds no more than 10 Characters.

Object variable refers to one of VB’s Objects

Dim a as CommandButton

Page 7: Types of Visual Basic Data Numeric Data Non Numeric Data

Ways of declaring Variables

Implicit Declaration:It is not a must that a variable should be declared before using.VB encounters a variable,it assigns a default variable type and value.

Explicit Declaration:Variable declared using Dim statement are called explicit declaration

Dim a as integer

Using Option Explicit:This forces the user to declare all the variables.This statement checks in the module for usage of any undeclared variables and reports an error to the user..

Option Explicit

(Tools menu-> Options ->Editor tab in Options Dialog box select the option Required Variable Declarations.)

Page 8: Types of Visual Basic Data Numeric Data Non Numeric Data

Assigning Values to Variables

After declaring various variables using the Dim statements, values can be assigned to those variables. The general format of an assignment is

Variable=Expression

The variable can be a declared variable or a control property value. The expression could be a mathematical expression, a number, a string, a boolean value(true or false) and etc. The following are some examples:

firstNumber=100 secondNumber=firstNumber-99 userName="John Lyan" userpass.Text = password Label1.Visible = True

Page 9: Types of Visual Basic Data Numeric Data Non Numeric Data

Scope of VariableVariable is scoped to procedure level or module level.

Procedure or local variable: Variable declared within the procedure using Dim and static keywords.

Static : They are not reinitialized each time VB invokes a procedure.They retain their values even after the procedure has finished.

Static a as integer

Module level:

These are variable by default available to all the procedure in that module.They are declared using Private or Public keyword

Private a as integer

Public keyword is used to make a module level variable to other modules.(Declare section)

Page 10: Types of Visual Basic Data Numeric Data Non Numeric Data

• In some cases, we need to add a suffix behind a literal so that VB can handle the calculation more accurately.

• & for long , ! for single, # for double, @ for currency, % for integer

• String with two double quotes and date,time with two #sign

Ex : Dim a%

NewValue$=“Pooja”

Page 11: Types of Visual Basic Data Numeric Data Non Numeric Data

Operators

• Arithmetic operators : +,-,*,/ and \ ,Mod,^

/ referred as floating point division

\referred as integer division

• Comparison:>,<,=,<>,>=,<=

• Logical : Not ,And, Or,Xor

• String Concatenation :&,+

Page 12: Types of Visual Basic Data Numeric Data Non Numeric Data

Data Type Conversion Functions

Cbool BooleanCbyte ByteCcur CurrencyCdate DateCDbl DoubleCint IntegerCLng LongCsng SingleCstr StringCvar VariantCverr Error

Page 13: Types of Visual Basic Data Numeric Data Non Numeric Data

Control Flow and Loop Statements

Page 14: Types of Visual Basic Data Numeric Data Non Numeric Data

If Statements

If Condition then

VB Statements

End if

If Condition Then

Else

End if

If Condition then

VB Statements

Elseif Condition then

VB Statements

Else

VB Statements

End if

Page 15: Types of Visual Basic Data Numeric Data Non Numeric Data

Select Case

Select Case Expression

Case Value1

VB Statement Block

Case Value2

VB Statement Block

Case Else

VB Statement Block

End Select

Page 16: Types of Visual Basic Data Numeric Data Non Numeric Data

The Do Loop formats are

Do While condition Entry Controlled

VB statements Block

Loop

Do Exit Controlled VB statements Block

Loop While condition

Do Until condition VB statements BlockLoop

Do VB statements Block

Loop Until condition

Page 17: Types of Visual Basic Data Numeric Data Non Numeric Data

For counter =sValue to eValue Step stepValueVB Statement Block

Next Counter

For each element in group

VB Statement statements

Next [element]

While ConditionVB Statement block

Wend

Page 18: Types of Visual Basic Data Numeric Data Non Numeric Data

With can be used in arrays or Collections.

With object

VB statement Block

end with

E.g.

With txtName

.Text=“Anubhav”

.Width=1000

End with

.

Page 19: Types of Visual Basic Data Numeric Data Non Numeric Data

To exit from for loop, Do loop, Sub Procedure/ function

Exit

Exit Do

Exit For

Exit Sub

Page 20: Types of Visual Basic Data Numeric Data Non Numeric Data

ARRAYS:

An array is a list of variables, all with the same data type and name. When we work with a single item, we only need to use one variable. However, if we have a list of items which are of similar type to deal with, we need to declare an array of variables instead of using a variable for each item.

Declaring Arrays

We could use Public or Dim statement to declare an array just as the way we declare a single variable. The Public statement declares an array that can be used throughout an application while the Dim statement declare an array that could be used only in a local procedure.

The general format to declare an array is as follow:

Dim arrayName(subs) as dataType

where subs indicates the last subscript in the array

Page 21: Types of Visual Basic Data Numeric Data Non Numeric Data

There are two types of Arrays in Visual basic:

Fixed size : Size remains same

Dynamic Array : size can be changed at run time.

Fixed arrays can be declared by giving the upper limit.

Dim or Public length(9) as Integer(10 elements)

Limit should be within a range of Long data type.

Dim length ( 1 to 10) as integer

Dynamic array can be initially declared and can add elements.

Dim a() Actual no of elements can be allocated using Redim statement

ReDim a(10)

This Redim statement can appear only in a procedure

ReDim a(4 to 10)

Page 22: Types of Visual Basic Data Numeric Data Non Numeric Data

When we execute Redim statement the current data stored in the array is lost.Without losing the previous data,we have to use the Preserve keyword with the Redim statement.

Redim Preserve a(Ubound(a)+1)

Multidimensional Arrays:

Dim marks(50,50)

Dim marks(50 to 100,50 to 100)

Any number of dimensions can be declared

When we use preserve keyword only the upper limit of the last dimension in a multidimensional array can be changed.No other dimensions or the lower limit of the last dimension can be changed.

Page 23: Types of Visual Basic Data Numeric Data Non Numeric Data

User Defined Data Types

Variables of different datatype when combined a single variable to hold several related information is called user defined datatype.It is useful when we create a single variable to hold different pieces of information.

The Type statement is used to create a user defined datatype in the general declaration section of a form or module.

Private Type Courseinfo

Coursename As string

Startdate as Date

Fees as Currency

End Type

Page 24: Types of Visual Basic Data Numeric Data Non Numeric Data

Variable can be declared using user defined datatype.Array on

these type can also be declared.

Dim a as Courseinfo

Dim a(10) as Courseinfo

To assign a value :

a.coursename=“MCA”

a(3).coursename=“bca”

User defined datatype can be passed to procedures also.

User defined datatype can only be private in form module while in standard module can be public or private

Class module also it can be private or public

 

Page 25: Types of Visual Basic Data Numeric Data Non Numeric Data

Constants• Constants are named storage location in memory.• Const statement is used to create a constant and

it requires initialization also.• Constants can be declared as public or private.• They can be declared in any module.• Public Const pie as single=3.14• In VB,predefined constants can be used

anywhere in the code in the place of actual numeric values

Ex: Form1.windowstate=2

Page 26: Types of Visual Basic Data Numeric Data Non Numeric Data

Procedures• Procedures are a group of related commands

that perform certain tasks.

• VB programs can be broken into smaller logical components called procedures

• Benefits:

Easier to debug

Code can be reused in other modules with little or no modification

• Two type : Event ,General

Page 27: Types of Visual Basic Data Numeric Data Non Numeric Data

Difference between twoEvent procedures are invoked in response to keyboard,mouse click or system action.Maximum number of events a control can have is fixed..

Event procedures declaration as Private by Default

General procedure is not executed unless explicitly invoked

General procedures can be private or Public

If the codes are repeating in event procedure,that can be placed in general procedure and can be invoked from event procedure.

General Procedures are further classified as:

• Sub

•Function

•Property

Page 28: Types of Visual Basic Data Numeric Data Non Numeric Data

Sub ProcedureSub procedure can be placed in standard,class and form modules.It do not return values.whenever the procedure is called the code between Sub and End Sub are executed.

Syntax:

[Private|public] [static] sub procedurename[(arg list)]

statements

end sub

Calling the procedure:

procedurename ar1,ar2,……

If there is no argument,only we have to write procedurename.

Arguments are not enclosed in the parentheses.

Page 29: Types of Visual Basic Data Numeric Data Non Numeric Data

FunctionFunctions are like sub procedure except they return a value to the calling procedure. There are two types of functions:

1. Built-in 2. User Defined Functions

A function returns a specific value to the calling block.

Syntax:

[Public|Private] Function functionName (Arg As dataType,..........) [As dataType]

End function

Calling the function:

Function call appears in an assignment statement

variable name =functionname(arg1,arg2,..)

Return type is an optional one.

Page 30: Types of Visual Basic Data Numeric Data Non Numeric Data

Property Procedure

It is used to create and manipulate custom properties.

It is used to create read only properties for forms,standard modules and class modules.

Three procedures:

Let : Sets the value of a property

get :Returns the value of a property

set :sets the reference to an object

Page 31: Types of Visual Basic Data Numeric Data Non Numeric Data

Event ProcedureAn event procedure is a procedure that contains the control’s actual name,an underscore(_) and the event name.

Event procedures are stored in a form module are private by default.

Maximum number of events a control can have is fixed.

Syntax:

Sub form_load()

statements

end sub

Page 32: Types of Visual Basic Data Numeric Data Non Numeric Data

Built-in FunctionsMsgbox() :

MsgBox is to produce a pop-up message box and prompt the user to click on a command button before he /she can continues. This message box format is as follows:

yourMsg=MsgBox(Prompt, Style Value, Title)

InputBox()

An InputBox( ) function will display a message box where the user can enter a value or a message in the form of text. The format is

myMessage=InputBox(Prompt, Title, default_text, x-position, y-position)

Page 33: Types of Visual Basic Data Numeric Data Non Numeric Data

Passing ParametersCode in a procedure needs some information about the state of the program to do its job.These are passed to the procedure as arguments. Argument data type by default variant .Other datatypes can be declared.

There are two types of passing arguments:

1. Pass by value 2.Pass by reference

Pass By Value:

In this only copy of a variable passed to it.

Any change in the value caused by the procedure,it won’t affect the

value and not the variable itself.

ByVal must be used to indicate this.

Sub Emp(Byval a as Integer) ………. End Sub

Page 34: Types of Visual Basic Data Numeric Data Non Numeric Data

By Reference:

Passing by reference is by default. Variable’s value can be permanently changed by the procedure to which it is passed by value. In this type a data type is specified for that parameter and when a value is passed,it must be of that type for that particular parameter.

We can pass the expression also,rather than a data type for a parameter.

Sub emp(a as integer) ………… end sub

Optional :

Argument can be defined as optional also.

Optional argument is given a default value within the procedure.

Optional argument should always be passed in the end.

Sub emp(a as String, Optional straddress as string)

……. End Sub

Page 35: Types of Visual Basic Data Numeric Data Non Numeric Data

Collections• A Collection object is an ordered set of items that can be

referred to as a unit.• An object that contains a set of related objects. An

object's position in the collection can change whenever a change occurs in the collection; therefore, the position of any specific object in the collection can vary.

• Similar to an array, stores related items• We can access the element using key• To use a collection we must declare a collection variable• Dim a as New Collection new keyword is used to create a new collection• Methods : Add,Remove,Item,Count

Page 36: Types of Visual Basic Data Numeric Data Non Numeric Data

• Add: It adds new Item into the collection. Collection.add value, key, before, after• Remove : It removes an Item from a

collection Collection.Remove index or key• Item : Returns the value of an item in the

collection collection.item(key or index)• Count: It returns the number of items in a

collection Collection.count

Page 37: Types of Visual Basic Data Numeric Data Non Numeric Data

• Whether the before or after argument is a string expression or numeric expression, it must refer to an existing member of the collection, or an error occurs.

• An error also occurs if a specified key duplicates the key for an existing member of the collection.

Page 38: Types of Visual Basic Data Numeric Data Non Numeric Data

User interface

• SDI

• MDI

• Explorer Style Interface