Wednesday, April 2, 2008

VB.Net : The Console Way

Though C# is my favorite language I never under estimated VB.Net. In fact my expertise on VB.Net paid me dividends in the programming career.

So event if you are a C# programmer I will strongly recommend learning VB.Net because the best .Net resources are in VB .Net. Examples are dotnetnuke and Asp.Net Unleashed by Stephen Walther.

Instead of taking the Visual Studio approach I am taking the Console and Notepad way. By doing so one can be well versed with the internals of the language.
Also instead of using the Visual Studio Command Prompt and using the Dos Command Prompt.
For doing so am copying a folder called v2.0.50727 from C:\WINDOWS\Microsoft.NET\Framework folder and pasting it in d:. This folder is the .Net Framework 2.o folder which is a an important folder. So instead of inadvertently removing any filed this approach is the better approach instead of going for PATH command.
I am renaming the folder to say DotNet.

Simple Console Application
Now open notepad and type the following code :

Public Class Hello
Shared Sub Main
System.Console.WriteLine("Hello")
End Sub
End Class

Save it as say 1Hello.vb.
Now open the command prompt cd to the newly rename folder that is DotNet and give the vbc (Visual Basic compiler) command followed by the file name.
If there are no errors it will create an exe with the same name that of the file name and you can invoke the exe by typing the name of the exe.














In this example we are creating an class library and inside the class library a Main method. Here the Main method is Shared which is equivalent to static in C#. It denotes that one need not create an instance of class for invoke this method.

It is followed by statement System.Console.WriteLine("Hello") which is meant for displaying Hello message in the command prompt.

Here System is a namespace, Console is a class and WriteLine is a method.

Memory Variable and String Concatenation

Public Class Hello
Shared Sub Main
Dim strName as String
System.Console.WriteLine("Enter your name : ")
strName = System.Console.ReadLine()
System.Console.WriteLine("Hello " & strName)
End Sub
End Class

For declaring a memory variable in VB .Net the syntax is Dim as DataType. The string concatenation operator in &.

Integer Memory Variable

Imports system

Class Cal
Shared Sub Main
Dim i,j,res as integer

Console.WriteLine("Integer 1 : ")
i= Console.ReadLine()

Console.WriteLine ("Integer 2 :")
j= Console.ReadLine()
res =i+j

Console.WriteLine("Sum : " + res.ToString())
End Sub
End Class

Function
Imports system
Class FunctionEg
Shared Sub Main
Dim res as IntegerConsole.WriteLine("Enter value : ")
res = Sqrt(Console.Read())
Console.WriteLine("Square Root is " + res.ToString)
End Sub

Public Shared Function Sqrt(i as Integer) As Integer
return i * i
End Function
End Class











Creating a Form

Imports System.Windows.Forms
Public Class frmHello
Inherits Form

Shared Sub Main
Application.Run(new frmHello)
End Sub
End Class

.Net is having a namespace called System.Windows.Forms. For creating a form you can create a class which inherits from Form class of System.Windows.Forms.
Application.Run(new frmHello)
Begins running a standard application message loop on the current thread, and makes the frmHello visible.

Class Constructor
Imports System.Windows.Forms

Public Class frmHello
Inherits Form

Dim btnHello As Button
Shared Sub Main
Application.Run(new frmHello)
End Sub

Sub New
Me.Text = "shalvin.com"
End Sub

End Class

Constructors are used to initialize a class and is the first method that is called when an instance of class is created.
To create a constructor in VB .Net create a procedure name Sub New.

Adding Controls to Form
Imports System.Windows.Forms

Public Class frmHello
Inherits Form

Dim btnHello As Button
Shared Sub Main
Application.Run(new frmHello)
End Sub

Sub New
Me.Text = "shalvin.com"

btnHello = new Button
btnHello.Text = "Hello"
Me.Controls.Add(btnHello)
End Sub

End Class

In this example we are creating an Object variable of type Button, setting it's text property and adding it into the Controls collection of the form.

Event Handling
Imports System.Windows.Forms

Public Class frmHello
Inherits Form

Dim WithEvents btnHello As Button
Shared Sub Main
Application.Run(new frmHello)
End Sub

Sub New
Me.Text = "shalvin.com"

btnHello = new Button
btnHello.Text = "Hello"
Me.Controls.Add(btnHello)
End Sub

Private sub btnHello_Click(sender As Object, e As EventArgs) Handles btnHello.Click
MessageBox.Show("shalvin.com")
End Sub
End Class



Related Blog


.Net Intermediate Language

No comments:

Post a Comment