Tuesday, June 23, 2015

Understanding Keywords and Syntaxes of VB.NET



Visual basic.net keywords and syntaxes were similar with the previous version of visual basic. There are two terms in object oriented programming, including visual basic.net:
1.   Method: generic name for command in visual basic. The Sub and Function are types of Method.
2.   Instance: When a class created, the object created was the instance from the class definition.
There are some keywords and descriptions  you need to understand while programming on OOP:
Keywords
Descriptions
Namespace
Classes collection that provides related ablities, for example System.Drawing contains classes related with graphic.
Class
Definition of object, including properties (variable) and method that can be a Sub or a Function.
Sub
Method that contains commands, enable data transferred as parameter and provide scope inside local variable, but doesn’t return value.
Function
Method that contains commands, enable data transfered as parameter and provide scope inside local variables and commands.
Return
Terminate Sub or Function currently executed. Can be combined with return value for function.
Dim
Declare and define new variable
New
Create new instance from object.
Nothing
To indicate that a variable has no value. Similar with null in other proramming languages, or in database.
Me
Reference to object inside the scope where the method executed.
Console
Application type that run on command line. Console app in dot net usualy needed for testing purpose only. Console also referes to class that manage command window and can read and write text data.
Module
Block of codes but not a class, can contain Sub or Function method. Used when there are only one code or data needed in the memory.
2.1  Compiler Options
Before you type codes, you need to set compiler options. There are many compiler options setup you can choose:
·         Option Explicit: This option don’t change from previous version of visual basic. If it’s activated or enabled. It will make sure that variabel declared explicitly. If you use Option Strict, this setting won’t be problem, because compiler don’t recognize the type of the undeclared variables. Basically, you dont have to use Option Explicit, unless you build a pure dynamic solution, where compilation time for typing unavailable.
·         Option Strict: When this option enabled, compiler will decide type of each variable. And if assignment between two variables need type conversion, eg: from integer to boolean, this conversion should be expressed explicitly.
·         Option Compare: This option make sure whether string has to be compared as string binary, or character has to be compared as text. Text comparation needs system to compare binary value saved internally before compared.
·         Option Infer: This option exists since VS 2008 and added because of LINQ requirement. When you execute LINQ statement, you will return data table that can’t be typed in statement Advanced.
2.2  Variable
Variable in programming language is a place to store information in computer memories. You can imagine a variable like an empty box, you can save some value in variable.
To add two numbers for example, you just write first number on an empty paper and put the paper on the empty box, then you write second number on an empty paper and put the second paper on the empty box.
In visual basic.net, this will be identical to this:
Dim number1 As Integer
Dim number2 As Integer
number1 = 3
number2 = 5
Codes above are codes in visual basic.net that describe how Visual Basic Net create and define variables.
Below are the explanations on several keywords used while defining variables:
Dim
Dim stands for Dimension”. This keyword used to declare variable type. By using this keyword, you are declaring a variable in visual basic.
number1
This can be compared with an emtpy box, label and it’s content. This is variable. In other words, this is your storage area, the place where you save your value.
After the DIM, visual basic will name your variable. You can name the variable with everything you like. But there are some reserved words you may not use.
As Integer
This keywod describe the variabel type. Here, you tell visual basic that the variable will be an integer. This will define the box’s size, different type will make the size of the variable different.
number1 = 3
The “=” character means you insert something to your variable. Here, you tell visual basic to set 3 to variable number1.
2.3  Variable Types
Variable has many types. In VB.net, each variabel type will have it’s own scope. And can consume spaces in computer memory.
Boolean
Boolean variable can contain True and False data. Inside the visual baisc, the value saved as number 1 and 0 it represent True and False.
Boolean value used as a conditional statement to determine whether some parts of the codes will be executed or not. It’s for controlling the flow of the program.
Char
Char value contains one character, eg: “B”. Char value can be any characters. It can span from 0 to 65.553. It can also used to save data from ASCII table. Eg: Z can be used on char variable, and number 90 used in variable location.
Byte
Byte variable contains positive number from 0 to 255. Because the range is very small, you have to use byte variable wisely, if this variable inserted with negative value, or value bigger than 255, this will cause an error.
Date
Date in visual basic can save date and time. Date should be inserted on #mm/dd/yyyy# format. For example May 23, 2016 will be declared as #5/23/2016#.
Visual basic use some mechanisms to work with Date variable. For example to count six months after today.
Decimal
Decimal variable can save up to 29 decimal places. If you use decimal number, it’s okay using this variable. If you use non decimal number, then use integer.
Double
Double variable used to save value that needs up to 28 decimal places. Double can be positive values from range 4.94065645841246544E-324 to 1.79769313486231570E+308 negative values from -1.79769313486231570E+30 to -4.94065645841246544E-324.
Double variable usually used to develop a scientific application and rarely used by regular developer.
Integer                                                                  
Integer variable used to save number between -2,147,483,648 until 2,147,483,648. This integer variable can’t save values that has fractional value/decimal. If the value has fraction, it will be rounded automatically.
Object
Object variable is a variable type that can be used to save all types of data. Because of this flexibility, object variable consume more memory space than other variable types. It’s not recommended to be used. Use specific type that you need.
Long
Long variable used to save all numbers ranged from -9,233,372,036,854,775,808 to 9,233,372,036,854,775,807.
Short
Short variable save number ranged from -32,768 to 32,767.
String
String variable save characters that can construct word or statements. String variable always opened and closed with quotation marks ("). For example "Hellow" or  "Hellow, how are you?".
Visual basic provides mechanisms to work with and manipulate strings. Table below details variables that hold number data type. :

Name in VB
Size
Range
SByte
8 bits (1 byte)
-128 to 127
Byte
8 bits (1 byte)
0 to 255
Short
16 bits (2 bytes)
-32,768 to 32,767
UShort
16 bits (2 bytes)
0 to 65,535
Integer
32 bits (4 bytes)
-2,147,483,648 to 2,147,483,647
UInteger
32 bits (4 bytes)
0 to 4,294,967,295
Long
64 bits (8 bytes)
-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
ULong
64 bits (8 bytes)
0 to 18,446,744,073,709,551,615

Variable types for floating points:
Name in VB
Size
Precision
Range
Single
32 bit (4 bytes)
7 digits
1.5 x 10-45 to 3.4 x 1038
Double
64 bit (8 bytes)
15-16 digits
5.0 x 10-324 to 1.7 x 10308
Decimal
128 bit (16 bytes)
28-29 decimals
1.0 x 10-28 to 7.9 x 1028

Other prediefined:
Name in VB
Size (bit)
Range
Char
16 bit (2 bytes)
One unicode symbol ranges 0 to 65,535.
Boolean
32 bit (4 bytes)
True or False
Object
32/64 bit (4/8 bytes)
Platform dependent (reference to object).
Date
64 bit (8 bytes)
January 1, 0001 12:00:00 AM to December 31, 9999 11:59:59 PM
String
80 + [16 * length] bit (10 + [2 * length] byte)
Unicode strings  with 2,147,483,647 characters maximum length.

To allocate a numeric value, you can use equal sign “=”. To allocate a certain numeric value to variable already declared, yo could use variable = certain_value.
For string and char variables, you can use quotation mark:
MystrVariable= "Some strings"
cMyhrVariable= "À"

For date/time, you can use hash/pound between value, by using this format #<month>/<day>/<year> <hour>:<minute>:<second> #. For example:
MyDateVariable= #7/4/2016 12:01:50 PM#
For other types, you don’t have to use anything.
myBytVariable = 1
mysbytVariable = -2
MyshrtVariable = 10S
MyushrtVariable = 10US
MyintVariable = 100
YouruIntVariable= 100UI
YourlnGVariable= 1000L
YouruLngVariable= 1000UL
sngVariable = 1.234F
dblVariable= 1.567R
decVariable = 1234567.89D
boolVariable = True
objctVariable = New Object

You can also allocate value to variable on it’s declaration, for example:
Dim myVariable As String = "Another String Values"
Visual Basic will allocate value from right variable to the left variable. Variabel in the left will take the value from the right variable. While the right variable’s value won’t change.

No comments: