Download presentation
Presentation is loading. Please wait.
Published byLeslie Lester Modified over 9 years ago
1
National Diploma Unit 4 Introduction to Software Development Data types, variables and constants
2
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 nature E.g. a=a+b is better than a=2+1 This allows us to decide the values of a and b within certain limits
3
Data types Depending on the limits we want and the type of data, we can choose to use a particular data type If none of the basic ones fit…you can even create your own (but you need to tell VB everything about it first)
4
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
5
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…………
6
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
7
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
8
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 –
9
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
10
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
11
Basic data types Date Anything from 1 st Jan 100 to 31 st Dec 9999 Covers most of the useful range!! Time information is also stored as hours, minutes and seconds Uses 8 bytes of storage
12
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
13
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
14
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
15
Variables Programs can’t do anything until you have reserved the space in memory that will hold values Dim 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
16
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
17
Variables Prefixes int = integerlng=long sng=singledbl=double byt=bytecur=currency dtm=datebln=boolean str=stringvnt=variant obj=object
18
Variables Variables also have scope and lifetime Dim intNumber as integer In this statement, Dim is a local declaration if it is associated with one control or sub procedure If it is in the general declarations area of the form, it has form-wide scope but is still private
19
Variables Public intNumber as integer This in the general declarations area of a form makes a variable publicly accessible This might be useful but don’t declare all variables public – much better to restrict their scope and keep them local to avoid confusion
20
Variables Static intNumber as integer Usually variables reach the end of their lifetime at the end of a sub procedure The memory they were using can then be “recycled” Static allows a variable to retain its value for the whole of the program This allows its value to be used by other bits of the program (normally it can’t)
21
Constants These are special items of data whose value will not be allowed to change at run time Examples include pi, the speed of light, acceleration due to gravity and possibly VAT etc Const dblPi as double Const sngGravity as single ….etc
22
Name the variable Write down some suitable identifiers for these variables: VATa telephone number Today’s date Whether or not the sun is shining Your name The price of a can of coke
23
Name the variable Remember to use meaningful names (no spaces or dots allowed) Use Hungarian notation to show the data type
24
Return to main Menu
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.