Parag’s Den

Dedicated to my adored friends…..

04- Basics of Programming

Posted by paragdongre on June 4, 2008


 

Variables

Variables are basically elements in a computer program that can take any assigned value. They can change from time to time, depending on the programmer and the user. For example, if a computer program requires a user to enter his or her name then the entity “name” will change from time to time depending on the user. The element “name” is considered a variable and can take on any value assigned to it. You will be using a lot of variables in your programming.

Data Types

Any kind of variable or element can take on a data type. There are several data types in Visual Basic. To name the most popular and the most useful are the following.

  1. String – this data type is made up of empty text, a single letter or several letters, or a group of words.
  2. Boolean – this data type is either True or False, or 1 or 0 representing the former and the latter, respectively.
  3. Byte – this data type can take on values from 0 to 255
  4. Integer – this data type can take on whole number values from -32,768 to 32,767
  5. Long – this data type can take on whole number values from -2,147,483,648 to 2,147,483,647
  6. Single – this data type can take on floating point numbers or numbers with decimals from -3.402823E38 to – 1.401298E(-45) and from 1.401298E(-45) to 3.402823E38
  7. Double – this data type can take on floating point numbers or numbers with decimals from – 1.79769313486231E308 to – 4.94065645841247E(–324) and from 4.94065645841247E(–324) to 1.79769313486231E308
  8. Variant – this data type can take on any kind of value. Use this data type sparingly because it takes up more than twice the memory of the other data types.

Declaring Variables

If you are ready to declare a variable in a program then you should use the following code:

 Dim X As Integer

Once a variable is declared in a program, you or the user can utilize it appropriately.

Memory Allocation for the Variables

Variables that have more complex data types take up more memory on the computer. Using too much memory can slow down a program or in some cases, may cause a computer to hang. Use your variables wisely and declare them appropriately. For example, if you have a variable that states the number of points of a basketball player in a game then it would be wise to declare “player_points” as byte instead of integer. No basketball player in history has scored more than 255 points in a single game and there is no such thing as a negative basketball point.

 

Operators

Operators are the basic way of manipulating data. The following operators are the most common operators that you will use in programming.

The “+” operator is used if you want to add elements together.

The “-” operator is used if you want to subtract one element from the other. The “-” operator is also used as a negative sign for negative values.

The “*” operator is used for multiplying elements.

The “/” operator is used if you want to divide an element with another element.

The “=” sign denotes that the values on the left side of the “=” sign are equal to the values on the right side.

A basic equation in a program may look like the code below.

 3 + x = 4<
 MsgBox (x) 

If this program is run, a message box will appear on the screen indicating the correct value of x, which is 1.

Writing and Running the Code

Interacting with the User

The intermediary between you, the programmer, and the users out there is the user form. You have learned how to put the textbox and the command button in the previous chapter. This time, you are going to use it. Make a user form with basic textboxes, labels, and a command button similar to the one below.

 

Writing the Code

Simply double click any element on your user form to begin writing code. To start off, double click “Button1” and see a code screen come up. When you type in the code for your program on this screen, make sure that you are referring to the right elements and variables all the time. You do not want to end up jumbling your data. If you are referring to data in Textbox1 then make sure that you don’t have the data of Textbox3 in mind. Once you get all things organized, your code may look like the one below.

Public Class Form1
 private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)  Handles Button1.Click

 TextBox3.Text = Val(TextBox1.Text) + Val(TextBox2.Text)

 End Sub
End Class

 

Visual Basic is very user friendly. As you can see, simply double clicking a feature on your user form will automatically insert code. In the code above, take note that the “Val()” code is used to differentiate text from numerical value. Try removing the Val() code and see what happens to the result. You will learn little tidbits of coding as you go along this tutorial so pay attention and absorb all this knowledge.

Running the Program

Once you have the code in place, press F5 or the “play” button on the task bar. You will enter “debug” mode. Test your program and see if it works. If you want to exit debug mode, simply press the “X” button on the upper right corner of the user form or the “stop” button on the task bar.

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: