national diploma unit 4 introduction to software development data types, variables and constants

Post on 12-Jan-2016

222 Views

Category:

Documents

2 Downloads

Preview:

Click to see full reader

TRANSCRIPT

National Diploma Unit 4Introduction to Software Development

Data types, variables and constants

Basic ideas

Computers are very good at doing sums….but only if we tell them what to do!Programs are best if they are kept general in natureE.g. a=a+b is better than a=2+1This allows us to decide the values of a and b within certain limits

Data types

Depending on the limits we want and the type of data, we can choose to use a particular data typeIf none of the basic ones fit…you can even create your own (but you need to tell VB everything about it first)

Basic data types

Integer A whole number in the range –32,768

to 32,767 Needs 2 bytes of storage space

Long A whole number in the range –

2,147,483,648 to 2,147,483,647 Needs 4 bytes of storage

Basic data types

Single Floating point numbers in the range

-3.402823E38 to –1.401298E-45 for negative values and 1.401298E-45 to 3.4o2823E38 for positive

Takes 4 bytes of storage This should be enough for anyone

but…………

Basic data types

Double Make yours a double if…… A single doesn’t provide the range or

accuracy that you need Takes 8 bytes of storage Bear this last fact in mind, if you don’t

need this level of accuracy, declare a single as it is more economical on storage

Basic data types

Currency Especially for financial data Uses 8 bytes of storage (Remember that a single only uses 4) Uses 4 decimal points for accuracy and

to ensure there are no rounding errors Range is –922,337,203,685,477.5808

to 922,337,203,685,477.5807

Basic data types

Decimal Any number scaled to a power of 10 Can be a whole number or just a

fractional part Range for whole numbers

79,228,162,514,264,337,593,543,950,335 + or –

For fractional numbers: 7.9228162514264337593543950335 + or –

Basic data types

Byte Used to store a number or character Number range =0-255 Character = 1 ASCII Stored in 1 byte so very economical

Basic data types

Boolean For true or false values Can be used for numbers

0=false Any other value = true

Uses 2 bytes of storage

Basic data types

Date Anything from 1st Jan 100 to 31st Dec

9999 Covers most of the useful range!! Time information is also stored as

hours, minutes and seconds Uses 8 bytes of storage

Basic data types

String For text and numbers that you don’t

want to do any maths on e.g. a NI number or telephone number

You can fix the length or let it be variable length

Fixing the length also fixes the amount of storage space

Basic data types

Variant When VB doesn’t know what data

type it is dealing with Uses 16 bytes for numeric data and

22 bytes overhead + the string itself for a string variable

Good programmers leave this one well alone It is wasteful of storage It doesn’t help with documentation at all

Basic data types

Object For when new objects are declared in

an object oriented program Uses 4 bytes of storage All new objects must belong to a

class which defines what characteristics they must have

The class is like a biscuit cutter and objects are the biscuits

Variables

Programs can’t do anything until you have reserved the space in memory that will hold valuesDim intNumber as integer This is a typical declaration statement It reserves a 2 byte slot of memory to

hold the value of intNumber

intNumber=3 Assigns the value of 3

Variables

Naming conventions We will use Hungarian notation which is

accepted a good programming practice Prefix the variable name with a 3 letter

identifier to indicate the data type The variable name should be meaningful -

good programmers can read the code of a program and know what it does

Variables

Prefixes int = integer lng=long sng=single dbl=double byt=byte cur=currency dtm=date bln=boolean str=string vnt=variant obj=object

Variables

Variables also have scope and lifetimeDim intNumber as integerIn this statement, Dim is a local declaration if it is associated with one control or sub procedureIf it is in the general declarations area of the form, it has form-wide scope but is still private

Variables

Public intNumber as integerThis in the general declarations area of a form makes a variable publicly accessibleThis might be useful but don’t declare all variables public – much better to restrict their scope and keep them local to avoid confusion

Variables

Static intNumber as integerUsually variables reach the end of their lifetime at the end of a sub procedureThe memory they were using can then be “recycled”Static allows a variable to retain its value for the whole of the programThis allows its value to be used by other bits of the program (normally it can’t)

Constants

These are special items of data whose value will not be allowed to change at run timeExamples include pi, the speed of light, acceleration due to gravity and possibly VAT etcConst dblPi as doubleConst sngGravity as single ….etc

Name the variable

Write down some suitable identifiers for these variables: VAT a telephone number Today’s date Whether or not the sun is shining Your name The price of a can of coke

Name the variable

Remember to use meaningful names (no spaces or dots allowed)Use Hungarian notation to show the data type

Return to main Menu

top related