Home Articles My Account Messages Tools Join
Tech Blog, ASP.Net, VB.Net, C#.Net, Programming Help, Help Guide
StellarPC.com | Understanding the data types for variable creation (VB.Net)
Understanding the data types for variable creation (VB.Net)
Written by: Justin Rich

Understanding the data types for variable creation

Knowing when to properly use certain data types will expedite your success with programming. Here is a short article explaining the proper methods of creating variables and the data types associated with them. Leaving data types off the declarations is possible, but without doing this, the program cannot properly identify the memory needs for the program initialization. This can cause the program to use too much or too little memory in the process. Sure, by today's standards of supercomputers, this is probably not a big issue, but it is still a good practice to do it properly and large scale systems can actually add up over runtime.

Integer - A whole number between -2,147,483,648 and 2,147,483,647 - This tends to be the most common whole number variable used, but if you know you're not going to be going very high in a counter, you can use a short instead.

Short - A whole number between -32,768 and 32,767. Less memory usage than an Integer.

Long - A whole number between -9,223,372,036,854,775,808 and 9,223,372,036,854,775,807. Much more memory usage than Integer or Short. Only use this if you're going to be calculating greater than the bounds of an Integer.

Single - The least precise of the floating point variable types. For basic fractional calculations that do not need to be within multiple decimal points (such as currency).

Double - A slightly more precise floating point variable type. Still use in basic fractional calcualtions because the next type is the most precise and has the least amount of difference from actual fractional floating points.

Decimal - A decimal type gets as close to actual point precision as can be allowed within the system. An example of the need for decimal over Double or Single would be 1/3. It's a never ending decimal, and the Decimal type will go closer to the exact value before rounding up. Your Singles or Doubles will retain less precision with the more math that is calculated against a number such as 1/3.

String - The generic variable for the collection of a number of alphabetic, numerical, and special characters. This data type holds as much of a string as the system can support.

Date - Represents a date between January 1, 0001 and December 31, 9999 and time sequence between 0:00:00 to 23:59:59. Dates can be very important in programming for time sensitive programs and I will show in a later article how to properly add & subtract time from a given date to calculate differences or predict future dates.

Boolean - This holds only the values of True or False. This is a great flag variable type for Yes/No, I/O, valid/invalid type processing. Probably one of the most used types in a lot of programs.

When instantiating variables, always be sure to assign them a value. After assigning a default value, be sure to remember what you gave it. For your numbers, give them a standard 0 or 0.0 and for strings, give them a "". Depending on the use of a Boolean, you should decide its initialization because if you are programming around the boolean starting as False and changing to True after your program does something successfull, you'll want it to be False from initialization. Setting it to True at the beginning could be cause for disaster and pointless debugging to find out why the program doesn't work. You can set Dates = Now, but not setting them usually results in the minimum date provided above. Here are the regular declarations:

Dim intVar as Integer = 0
Dim shrtVar as Short = 0
Dim lngVar as Long = 0
Dim sngVar as Single = 0.0
Dim dblVar as Double = 0.0
Dim decVar as Decimal = 0.0
Dim strVar as String = ""
Dim dtVar as Date = Now
Dim blnVar as Boolean = False

It's important to use a version of Hungarian notation for your variable prefixes. This way, as you're using the variable throughout the program, you will always know from the variable name what type the variable was initialized as. Hungarian notation represents the 2 to 4 letter abbreviations used before the Var name given, such as str for String. As long as they make sense, they don't have to be exact, but you will find these common amongst most programmers. Stray from using variable names such as i, j, x, y, or z. Sure, we see this all of the time in textbooks teaching us the languages, but it's just not proper use of variable naming when actually programming real-world applications.
New Post Next 50 | Previous 50
Post# Subject: Posted By: Time:
New Post Next 50| Previous 50