February 20th, 2013, 09:42 PM
Data variable, constant and data types
I always find it hard to understand data variables, constants and data types
February 21st, 2013, 01:01 AM
Re: Data variable, constant and data types
A Data type identifies the type of information you’d like to store inside a variable. By instructing the compiler what Data Type you would like to use, you do two things :
1. Create the necessary space in memory for that particular variable
2. Ensure that your variable can hold only the specific data that you have specified. For example: When you create a variable soleley intended for a date, you will not be able to enter any other type of information inside it
A variable is a container where a piece of information can be stored. This container exists solely in memory (computer memory). You could store anything inside that designated memory location be it a name, a whole document, a number or a date. When you create a variable, you create space in memory for that variable’s contents.
How do I create Variables?
In order to create a variable, you need to make use of the Dim statement, here’s an example :
This little statement creates a variable named intAge. intAge is created with an Integer (whole number) datatype.
Dim intAge As Integer
Here is another Example :
strName is the name of our variable. String is our variable’s data type. This means that strName can accept any alphabetic and numeric input. Do not get confused now with numeric input. Remember, as I mentioned before Data Types are quite strict in what they allow into a variable. Strings can accept both Alphabetic and numeric input; the difference is that it doesn’t see the numeric input as actual numeric data that should be used in calculations.
Dim strName As String
Storing values in a variable
Stroing a value in a variable is as simple as :
With the small examples above I stored a vlaue into each of our variables we created earlier. Just a note: you will not necessarily store all your data in your variables simultaneaosly or independantly. It just depends on when you need to store which info.
intAge = 35
strName = “Hannes”
Another note: Remember I spoke about String Data types? Well, look closely to the strName variable assignment I did above. I made use of “ and “ around the data. If I did this :
It would also have been a legal string; obviously using numbers for a name doesn’t make sense, but this was just a silly example.
strName = “Hannes 123”
Constants are also a container where a piece of information can be stored in memory. So what is the difference between Constants and Variables
Constants vs. Variables
The difference is simple. A Variable, as its name implies, can hold varying values. This means that you can change the value of a variable umpteen times and it will still store each change at the same location.
Constants however refer to values that cannot be changed during program’s execution. This means that once you store a value as a constant, that value cannot be changed, ever.
How do I create Constants?
You create a Constant as follows :
With Constants you name your constant and immediately assign it a value. That value cannot ever change, so if you were to assign a different value into your constant ( as shown in the next tiny example) you will get an error.
Const intMonths = 12
** This is part of a book I'm trying to write...
Click Here to Expand Forum to Full Width