Sep 26, 2009

Discover the Error and Correct It

Visual Basic has one of the most useful tools in helping you discover and correct your errors. Starting from highlighting the statement with error, giving you a quick watch of the value of the variables at that stage or even giving you the best menomic message at the run time that will help you identify the error.

In addition to all of this…the debugging tools provided is amazing and very useful. I will not go into debugging detail now.

The useful approach that I have used is the following:

  1. Read the error message thoroughly and try pressing F1 to get additional help
  2. The second step would be to identify the expected value of the variables as well the current values (hover with the mouse over them)
  3. Bing on the .NET for possible reasons as well as solution
  4. Last Resort…ping me or any other friend for help…

Happy Programming :)

Error Handling in VB.NET

Visual Basic has three main error handling techniques while writing code.

On Error Go To technique allows you to go to a special section in your code to handle the error and exit gracefully. An example is added below.

Sub CalculateD()

On Error Go To HandleDivisionByZero

Dim a as integer = 0, b as integer = 5

Dim d as decimal = b/a

Console.WriteLine (“B divided by a is = “ & Str(D))

Exit Sub

HandleDivisionByZero:

MessageBox (“Division By Zero. Exiting”)

Exit Sub

End Sub

On Error Resume Next technique is simply ignoring the current statement with the error and start from the next statement directly. An example is below:

Sub ResumeWork()

On Error Resume Next

Dim a as integer, b as integer

a = 5: b= 0

dim c as decimal

c = a /b

Console.Writeline(“Finished”)

End Sub

This subroutine would simply ignore the line where c = a/b even that there is a division by zero and continue to print the word Finished as if there is nothing wrong.

 

The two previous technique were simply the old techniques. However, .NET has introduced the Java old technique of try and catch where you can handle all kind of errors and it is extensible.

Try and Catch techinque is a well structured one and allows for even other vendors like Oracle to introduce their own catch blocks.

An Example of this technique is provided below:

Sub CalculateD()

Dim a as integer = 0, b as integer = 5

Try

Dim d as decimal = b/a

Console.WriteLine (“B divided by a is = “ & Str(D))

Catch MyException as exception

MessageBox (“Division By Zero. Exiting”)

Finally

‘ A Comment The programmer can do whatever cleaning he need here

End Try

End Sub

You can see from this code that the technique does not include jumping as well as you are able to catch the exception and handle it as needed. Oracle for example has added another exception called OracleException to handle errors returned by their own database and display their exact exceptions instead of using the .NET exception.

Programming Dilemma

A famous pizza man said one “Success is a horrible teacher”. You should know that pizza man because he ended up as one of the most infulencial guys in our modern history. Bill Gates.

Especially in programming, the best way to learn is when you commit, discover and correct mistakes as you go along.

In programming there are two kinds of errors that could happen.

Design Time (Compile Time) errors happens when you are simply writing the program in visual studio. Fortunatly these errors are highlighted automatically by underlining them using red and green line (read mean error and green mean warning)

Run Time errors occurs when the programming is running (the most commonly used term for this is bugs). All of us want to write programs that are bug free. However since we are humans we can not achieve this goal. the good news though is that with practice your are more likely to write near bug free programs. As somebody has said “programming is easy, it is finding all the problems that is hard”.

 

A recommended technique is simply “Write a little, Test a little”. Simply write your piece of code and make sure after testing it that it behaves as expected.

Sep 25, 2009

What is a Variable

a variable is a container that has a name and holds some value at the end. In VB.NET you dimesion variables before you can start using them. The dimensioning process simply tell VB.NET that you want a variable before using it.
VB.NET allocates a space in memory for each variable you dimension so that you can put there whatever you like. However since VB.NET does not know what you want to store, you have to give to the variable a datatype which restricts what you can store in this variable.
 
Datatypes keep VB.NET and you in harmony. Since the computer has to make special arrangement for the space in memory based on whether it will store a string of text, a numerical value or may be even a picture.
 
There is a lot of datatypes to choose from but I will explain these most commonly used one's before.
Integer: It is a variable type that allows you to store numerical values without a decimal point that range between  -2,147,483,648 to +2,147,483,647. Mostly commonly used as a counter for something like this example.
Dim NumberOfChildren as integer = 5
Boolean:  It is a variable type that allows you to store one of two values (true or false) used to indicate a status. For example
Dim HasChildren = True
DateTime: This is a variable type that allows you to store date and time information as accurate as it can be up to the ticks of the second. An Example would  be
Dim CurrentMoment as DateTime = Now()
Double: This variable will be able to store numerical values of all sizes approzximatly. It can store numbers with decimal point also. the range of values is 5.0 x 10-324 to 1.7 x 10308 which is a pretty long range for you. An example would be
Dim SolarDistance as Double
Decimal: When dealing with some banking issues decimal proves to be more useful when you want a precise number of decimal places. It's not as accurate as the Double Type, though.
Dim Balance as Deicmal = 18732.321
String: Very useful in storing large amount of text. It is able to store a unicode string with a maximum length of 2,147,483,647 characters. I think that this is enough to store any type of text you have (except if we are talking books)...
Dim FullCustomerName = "Nidal Bin Hasan Bin Ahmed Bin Tarek Bin Omar Bin Enough"
 
You can see from this succint list that VB.NET has the power to store any type of variables that you have (you will see different types in the future).
 
There is a nice addition to these variable with .NET 3.0 which is the nullable data type. I will be posting another entry about them.
 
See you in a later blog post.
 

Expressions and Statements

Expressions are simply a statement where you have included a calculation that should end as a result (numerical, character or as a boolean also).
Example of expressions are given below:
5+6  (Sample Numeric Expression)
x+y
"Nidal" & " Arabi"  (Sample String expression)
x.ToString() + " Is the Number of Children"
X > Y (Sample Boolean Expression)
 
Statements on the other hands are the building block of the programming when dealing with VB.NET
They are made up of (or combination of)
  • keywords-i.e. reserved words for the VB.NET language like Dim, If, Else, etc...
  • Operators (Explained in previous post)
  • Variables (Explained in previous post)
  • Constants (Will Be explained when need arise...But think of them as variables with a specific single value that should not be changed once assigned)
  • Expressions (Explained Above)

There are three main different categories of statements

  1. Declarative: When you define a variable like Dim EyeColor as String
  2. Assignment: When You assign a new value to a variable like EyeColor = "Brown"
  3. Executable: A statement that usually instruct the computer to do something special like If A  > B Then MessageBox("A is Greater then B")

To summarize

  • Each statement is simply a command to execute
  • Each statement can be on its own line. Statements can also be grouped on a single line by separating them with a column. Example of two statements together would be X = X + Y : Y = Y * 2
  • Sometimes you may also need to span a single statement on two lines. You can do this simply by using the underscore character. Example: Gross Income =  _
    TotalIncome – Tax.
  • A sequence of statement can make you programs do beautiful things. :)

Operators in Visual Basic

An operator performs a function on one or more operands. For example, we add two variables with the "+" addition operator and store the result in a third variable with the "=" assignment operator. Visual Basic comes with many built-in operators that allow us to manipulate data.

Operators in VB.NET comes in different categories: (All in order of precedence)

Arithmetic Operators

Operator To Do
^ Exponentiation
- Negation (used to reverse the sign of the given value, exp -intValue)
* Multiplication
/ Division
\ Integer Division (Division without remainder 5\2= 2 and not 2.5)
Mod Modulus Arithmetic
+ Addition
- Subtraction

String Operators

Concatenation operators join multiple strings into a single string. There are two concatenation operators, + and &.

Comparison Operators

A comparison operator compares operands and returns a logical value based on whether the comparison is true or not. Please find them below.

= Equality Operator
<> Different or Not equal
< Less than
> Greater Than
<= Less Than or Equal
>= Greater than or equal

Last but not least Logical Operators

The logical operators compare Boolean expressions and return a Boolean result. Below a list of them.

And compare two Boolean expressions and return true if both of them are true else return false
Or compare two Boolean expressions and return false if both of them are false else return true
Not negate true to false and vice versa.

 

Happy VBNetting

VB.NET Start Series

Starting Today I will be posting a series of topics in VB.NET as part of my learning and exprience. Keep Coming Back to see more Wink

Sep 23, 2008

Need Some Microsoft BI Concepts

Another interesting article can now be found ....
On the topic of BI at SSAS (Sql Server Analysis Services...)

http://aspalliance.com/1728_SQL_Server_Analysis_Services_Concepts.all

Enjoy intelligence on Microsoft

Sep 16, 2008

How Does SQL Server Analysis Do the Job

You can know right away from the picture that Microsoft Has done a great job in SSAS....You can read this article for fun on the Asp Alliance

Sep 14, 2008

Hierarchy Data Type

Another great feature for Microsoft SQL Server Level

What about your family tree ....Want to store into your SQL Server

got it using the new data type
http://aspalliance.com/1722_SQL_Server_2008_New_Features__Hierarchy_Data_Type

Check this link

Sep 11, 2008

SQL Server New Article On ASP Alliance

Dear SQL Server Lovers

Follow this link to read and rate my new article on SQL Server new features -  Table-Value Parameter

Sep 5, 2008

Grouping Sets

Any SQL Server Lover Here

There is another article on SQL Server 2008 Features....Grouping Sets

Follow this link http://aspalliance.com/1716_SQL_Server_2008_New_Features__Grouping_Sets.all

New Article On Asp Alliance

Dear Microsoft Lover,

here comes another article on the New Features of SQL Server 2008 about using grouping sets....

Here you can find the link
http://aspalliance.com/1716_SQL_Server_2008_New_Features__Grouping_Sets.all

Sep 1, 2008

Another Article On SQL Server New Features

I have published another article on SQL Server 2008 New features as it applied to date and time variables...
Check this the link for more info
http://aspalliance.com/articleviewer.aspx?aId=1717

Aug 25, 2008

New Article- SQL Server 2008 New Features (Row Constructors)

I just published a new article on a new SQL Server 2008 Features (Row Constructors) on ASPAlliance....You can see the new article using this link

http://aspalliance.com/articleviewer.aspx?aId=1709

Want To Learn VB Easy Way

VB.NET is one of the easiest langauges to learn and Microsoft has made available some videos to teach you concepts of of this language....
Download them for free from MSDN here

LebDev Community Night

Dear All Lebanese Developers
I would like to welcome you to register for a new community night given by Ralph Varjabedian (Telephone.com Team Leader)
About SQL Injections Hands On

The location , time and date are
Friday, August 29, 2008 - 6:00-8:00 PM
Microsoft office

visit the www.lebdev.net to register for the event. see u there

My Articles RSS Feed

As you know by now, I write articles on Microsoft Technologies....You can always get a list of my articles at the following link http://aspalliance.com/author.aspx?uId=61925

New Service Pack

Microsoft has introduced a new service pack for Mircosoft Visual Studio 2008 as well as .NET framework 3.5....You can access service pack 1 using this link http://msdn.microsoft.com/en-us/vstudio/products/cc533448.aspx

Merge Statement Tutorial

You can check my new article publisghed on www.aspalliance.com on the new merge statement in MSSQL Server 2008 using this link http://aspalliance.com/1712_SQL_Server_2008_New_Features__Merge_Statement