First VB.NET Program

Unknown | 3:14 AM |

To start of with any language it’s always worth writing a simple program, which actually
does nothing but displays a “HelloWorld” string in the screen. Taking this simple
program we will try to figure out how .NET framework delivers a successful
HelloWorld.exe. Well to write such a complex program we will go for our favorite editor,
the choice is unanimous, it’s “Notepad”.



'This is the famous HelloWorld Program written using VB.NET
Namespace HelloWorldSample
'Definition of the Class
Public Class HelloWorld
'entry point method for the Class
Public Shared Sub Main()
System.Console.WriteLine("HelloWorld")
End Sub
'end of Class Declaration
End Class
'end of the Class Module
'end of namespace
End Namespace
Now let us spend sometime in examining the HelloWorld program to find out what’s new
in writing code through any .NET language.

The lines in the program that starts with a ‘(single quote) are comment entries like in
other programming languages which are excluded in the compilation process. Like VB
the way in which comment entries are represented remains the same in VB.NET.
Namespace HelloWorldSample - The keyword “Namespace” is new to some
programmers who are not familiar with C++.

‘Namespace’ – a keyword in .NET is used to avoid name collisions i.e. For example, you
develop a library which has a class named “File” and you use some other library which
also has a class named “File”, in those cases there are chances of name collision. To
avoid this you can give a namespace name for your class, which should be meaningful. It
is always better to follow the syntax (MS Recommended) given below while giving
names for your namespaces

CompanyName.TechnologyName

However the hierarchy can be extended based on the implementation of the classes in the
library.

Public Class HelloWorld - This is the class declaration in VB.NET; the interesting thing
for VB developers is that VB.NET is a fully object-oriented language (so everything is a
Class here) . The class always ends with an “End Class”.

‘Public’ - is the modifier to determine the scope of the class (for other modifiers refer
.NET framework SDK documentation or later parts of this tutorial). HelloWorld is the
class name given for the class. Consumers of the class will be accessing through this
name only

Public Shared Sub Main () - This is called as the entry point function because the
runtime after loading your applications searches for an entry point from which the actual
execution starts. C/C++ programmers will find this method very familiar (VB
Programmers remember Sub Main). All Applications (exe) must have a definition for the
Main Method. Try removing the Main method from your application and the compiler
will complain that "No Start Point Defined". This means that the Main Method is the
starting point of any application, in other words When you execute your Application
"Main" method is called first automatically.

'Public' - This is the Access modifier for the Method. Since the Main method should be
accessible to everyone in order for the .NET Runtime to be able to call it automatically it
is always defined as public.

'Shared' - indicates that the method is a Class Method. Hence it can be called without
making an instance of the class first.

Now its time to compile and execute this complex program. To compile the above piece
of code you can use VB.NET compiler. To run the VB.NET compiler make sure you set
your path variable to point to the place where your VB.NET compiler is available. (To
set a new value in the path variable, go to control panel and double click System icon,
then choose advanced tab and click Environment Variables button to add or edit the
environmental variables)

Figure shows compilation of the HelloWorld program for VB.NET
The compiler used here is “vbc”, which is a visual basic .net compiler accepts the source
file “HelloWorld.vb” compiles the same to produce a program that’s not true executable,
instead it generates something called assembly. Here the VB.NET compiler produces a
Managed Code/Intermediate Language (MSIL) format that uses instructions which are
CPU-independent.

Category:

About http://dotnetvisual.blogspot.in/:
DOT NET TO ASP.NET is a web application framework marketed by Microsoft that programmers can use to build dynamic web sites, web applications and web services. It is part of Microsoft's .NET platform and is the successor to Microsoft's Active Server Pages (ASP) technology. ASP.NET is built on the Common Language Runtime, allowing programmers to write ASP.NET code using any Microsoft .NET language. create an application very easily ....