Sunday, May 15, 2011

Variables

Programming is based on mathematics. Variables are used in the same manner as in an algebraic formula: storage. The only thing that is different is that they are much more advanced in what they can do. Variables are the most basic of objects used in programming.

Like in math, we need a place to store numbers. That is where variables for the type short, long, integer, and double are used.
short: Limited to being between -32768 and 32767
long: Limited to being between -2147483648 and 2147483647
integer: Limited to being between -2147483648 and 2147483647
double: Limited to numbers with decimals
The main difference between the different types is that they use different amounts of memory. In most cases short variables will suffice, unless you are working with really big numbers. Most people seem to default to integers since memory is not very limited any more, yet I still like to use shorts whenever possible.

Now what if you need to declare something other than a number? What else can variables store? Strings of characters are another common thing for programmers to store. A string in simply a list of characters that make up text.
This is a string.
There are to variable types that can store characters:
Char: Stores one character.
String: Stores an array of characters.
Again, the difference of the two types above is that the memory required to store one character and storing a list of them is very different. Most commonly a string is used verses a char.

Sometimes you may need to keep track of whether something is true or false. This is the core of a computer system relating directly to the binary in which it operates. As a variable, we call them of the type boolean. Any variable of the type boolean can store a value of either true or false.
Additional, you can store advanced objects into variables. Advanced objects are ones that you create using classes and structures. While these advanced variables might not seem very useful at first as you begin programming, later with more advanced programs you will find that they are very useful and save a lot of time.
Structures: As the name implies, a structure is an object that contains things, much like a house or a building does. While these things are not like those of a house, they are important to the programmer. The structure is a collection of other variables, like shorts and strings. The big difference between the basic variables and a structure is that a structure holds much more information then a basic variable.
Classes: A more advanced structure is called a class. Each class is similar to a structure as they hold many different variables, but in addition a class also can hold subroutines and functions that can be used to ensure that the data stored in the class is correct. This is very handy when it comes to classes that store calculated data by having the calculations occur automatically when a new object is dimensioned.

Visual Basic:
Dim number as short
Dim trueFalse as boolean
Dim myText as string
Structure customStructure

Dim name as string
Dim age as short
Dim phoneNumber as string

End Structure

Dim myStructure as New customStructure
Public Class customClass

Dim name as string
Dim age as short
Dim phoneNumber as string

Sub New(name as string, age as short, phoneNumber as string)

Me.name = name
Me.age = age
Me.phoneNumber = phoneNumber

End Sub

End Class

Dim myClass as New customClass

No comments:

Post a Comment