ALIVE SOFTWARE'S
Learn Some thing interesting topics in Vb .. For beginners Only.
============================================================================
============================================================================
Give us feed back on vic_Xcali_ky@yahoo.com
NAMING
VARIABLES
By vicky Jadhav
It is a good programming practice to give descriptive
names to your variables. In this way the variable's purpose can be more easily
understood.
Let's change the variable name of the previous example:->----->
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Dim intCount As Integer
Command1.Caption = ""
For intCount = 1 To 10
Command1.Caption = Command1.Caption & intCount
Next intCount
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Notice that the variable name now better describes what
the variable is used for. Also, by putting an abbreviation of the variables type
in front of the variable name you can always know this variable stores integer
values.
The first character of any variable must be a letter. The other characters in
the variable name can be either letters or numbers.
Certain variables can have one of these symbols as their last character:
%,&,,#,$. More on this later.
No variable can have the same name as a Basic reserved word.
Reserved words have special meaning to the Basic language. The following are
examples of Basic reserved words: DIM, FOR, TO, STEP, NEXT.
STARTING A NEW PROGRAM
If you have entered a program in Basic and wish to start a new one there are
basically two ways to do this:
If you wanted to save the old program, you could simply create another button
for your new code.
If you do not need the old program simply highlight the text of the old program
and delete it by pressing the [Delete] key.
Since we want to try another program choose one of the previously mentioned
options now. Then enter this program:->------->
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Dim lngSalary As Long
lngSalary = 10000
MsgBox "" & lngSalary
lngSalary = 40000
MsgBox "" & lngSalary
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Run the program to see what happens.
Notice that the variable "lngSalary" can be changed to any integer number during
the course of the program.
To assign a value to a variable you use the equal sign "=".
VARIABLE TYPES
Whenever you use a variable in Basic you should assign it a variable type.
There are six basic variable types, each having a special symbol to identify
them:
% - Integer : -32,768 to 32767
& - Long Integer : -2,147,483,648 to 2,147,483,647
- Single Precision : see VB help
# - Double Precision : see VB help
$ - String : see VB help
@ - Currency : see VB help
There are five other common types that do not have symbols:
Byte - 0 to 255
Boolean - True or False
Date - 1/1/100 to 1/31/9999
Object - any object reference
Variant - any data
VARIABLE DECLARATIONS
Variable typing is very important in good programming. Proper typing helps
prevent errors or bugs in your programs, which can result in unusual behavior.
Notice the first line in the previous program we looked at:->------>
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Dim lngSalary As Long
lngSalary = 10000
MsgBox "" & lngSalary
lngSalary = 40000
MsgBox "" & lngSalary
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
This line is an example of variable typing.
Variables should always be typed at the very start of your program.
To type a variable you must declare it with the DIM keyword and then assign a
type to the variable. In this case the variable's type is "Long".
STRING VARIABLES
In programming, text data is referred to in terms of strings. Strings are any
sequence of characters enclosed in quotation marks.
For an example look at this program:->-------->
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Dim strFirstName As String
Dim strLastName As String
strFirstName = "Mike"
strLastName = "Huete"
MsgBox strLastName & ", " & strFirstName
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
FUNCTIONS
Functions are not variables but often take variables as input. When a function
is called it completes a task and then outputs a value.
InputBox is a function:->--------->
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Dim strRad As String, strPrompt as String
strPrompt = "Enter Radius: "
strRad = InputBox(strPrompt)
MsgBox "Area of circle: " & 3.14 * Val(strRad) ^ 2
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
In this example, the InputBox function takes the strPrompt
variable as input, requests a number from the user and returns a value that is
put into the strRad variable.
In order to use the radius obtained from the InputBox command, the string value
returned was converted to a numeric representation using the Val() function.
DATE & TIME
Functions do not always require input variables. Two such functions generate the
current date and time: ->----->
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
MsgBox "Today's Date is: " & Date$
MsgBox "The current Time is: " & Time$
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
A constant is like a variable except that a constant's
value can never change:->---------->
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Const PI = 3.14
Const PROMPT = "Enter Radius: "
Dim strRad As String
strRad = InputBox(PROMPT)
MsgBox "Area of circle: " & PI * Val(strRad) ^ 2
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Here we have two constants, a numeric constant, PI, and a
string constant, PROMPT.
Many programmers like to use all capital letters for constants so that they
won't be mistaken for variables.
ARITHMETIC
With Basic you can easily perform all manner of arithmetic operations. Look at
this example: ->---------->
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Dim intResult As Integer
intResult = (2 + 2) 'addition
MsgBox "2 + 2 = " & intResult
intResult = (3 * 4) 'multiplication
MsgBox "3 * 4 = " & intResult
intResult = (4 - 2) 'subtraction
MsgBox "4 - 2 = " & intResult
intResult = (6 / 3) 'division
MsgBox "6 / 3 = " & intResult
intResult = (2 ^ 3) 'exponents
MsgBox "2 ^ 3 = " & intResult
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Here the values of arithmetic expressions are appended to
strings. This results in one string value that is used as input to the MsgBox
function.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
well friends tell me about this topic if it hepls u
then its good for me haye any feedback @
vic_Xcali_ky@yahoo.com
home
______--------------______--------------_______------------_______------------_______-------------______-------------_____