Saturday, April 7, 2018

Visual Basic .NET Debugging, Comments, Variables

http://www.visual-basic-tutorials.com/ 

As soon as you create a new project, it is ready to run.
However if you run it immediately it will display only an empty form containing no controls.
This form will compile and run smoothly because there is nothing to be debugged/validated yet.

As soon as you add some controls to your form and write appropriate code for them you will want to test your application. To do that you simply start debugging!

There are several ways to start debugging:
1. Open the Debug menu and select "Start Debugging".
2. If you’re using the Visual Basic environment settings, you can simply press F5
3. Click Debug item in the toolbar as shown on the image below:



If there are no errors your app will run shortly after you started debugging.



Please watch the Output Windows as this window displays compilation results and output produced by Debug and Trace statements.


Comments

The comments are one of the few typographic code elements that make a programs structure a bit easier to understand.
They do not execute and they are ignored by the compiler but are an important part of how you organize your code.

Generally speaking, comments can help you as well as other developers (at a later date) to understand the code purpose.

The comment starts with a single quotation mark (') and everything until the end of the line is part of the comment and as mentioned ignored by Visual Basic.

NOTE: you cannot use line continuation characters to make a multi-line comment nor you can have block comment as they are not supported in VB.NET

If you want to comment or uncomment a large block of code quickly, just select the code and then open Edit > Advanced > Comment Selection and Uncoment Slection respectively.

You should always use comments to make your code clear and understandable.
?
1
2
3
4
5
6
7
8
9
10
11
12
' checking if the username and password are valid
If Username.Text = "admin" AndAlso Password.Text = "mypwd" Then
    Dim mForm As Form = MainForm ' instantiate the MainForm
    mForm.ShowDialog() ' display the main form
    Me.Close() ' close the login form
Else
    ' incorrect username or password provided
    Username.Text = String.Empty ' reset the username textbox
    Password.Text = String.Empty ' reset the password textbox
    ' let them know that username or password is incorrect
    MessageBox.Show("Incorrect username or password. Please try again!")
End If

Generally speaking, the variables are among the most fundamental components of programming.

A variable is an object that stores a value which can be a number, letter, string, date etc.

If a variable contains a value, the program can manipulate it. Meaning, it can perform arithmetic operations on numbers, string operations on strings (concatenate, calculate substrings, finding a match within string and so on), date operations (find the difference between two dates, add a time period to a date), and so forth.

The variable's behavior is determined by:
1. Scope - it indicates the scope levels from where the variable can be accessed (private, public etc.)
2. Data type - can be an Integer, String, Boolean etc.
3. Accessibility - determines what code in other modules can access the variable
4. Lifetime - determines how long the variable value is valid
For instance, any variable declared inside a subroutine has scope equal to the subroutine and cannot be accessed by the code outside of the subroutine.


                   Private Sub DoSomething()
                       Dim InsideVariable As String = String.Empty;
                         ' Do Something with the variable
                  End Sub

              Console.Write(InsideVariable)
                  ' ERROR: variable is not in scope (can't be accessed from outside)
In Visual Basic you use the keyword Dim to tell Visual Basic officially that you want to declare a variable.
You can avoid the Dim only if you specify Private, Public, Protected etc. however a variable delared using a Dim keyword is Private by default so the next two declarations are identical:

              Dim MyString As String = String.Empty
             Private MyString As String = String.Empty

No comments:

Computer Terms

The termination of the process due to a program or system fault      -      Abend(abnormal ending) To terminate a process before completion....