DotNet Tutorials

Server Intellect

 Using and creating Classes and Namespaces in VB.NET

In this tutorial, we will discuss the uses and benefits of namespaces and classes in VB.NET. Much like most object-oriented programming languages namespaces and classes are used to organize processes in large web applications. We will cover how to create and use namespaces and classes.

In general, namespaces are equivalent to groups. Namespaces can also be nested meaning one can be placed inside of another namespace. A good example of this is when you use the Imports (VB.NET) or Using (C#) to use classes, structures, etc. for Regular Expressions, SQL Database Manipulation, or File Management. All of these elements are organized with namespaces to prevent loading unnecessary elements during each web request.

Yes, it is possible to find a good web host. Sometimes it takes a while. After trying several, we went with Server Intellect and have been very happy. They are the most professional, customer service friendly and technically knowledgeable host we've found so far.

First you can start a new Web Application in Visual Studio 2008. Add the ASP.NET App_Code folder to your Web Application. Now add a new class file. Depending on whether you are in Visual Basic or C# mode, the file's extension will be vb or cs. Now we will add a namespace and a class inside of that namespace.

Namespace Namespace1
     Public Class Class1
           'class code
     End Class
End Namespace

Now that we have created this class inside of this namespace, we will have to learn how to call it. This class won't be accessible by the application unless it called by the namespace or included at the beginning of the application. We will show an example below.

Dim theClass As Namespace1.Class1 = New Namespace1.Class1()

--- or ---

Imports Namespace1
Dim theClass As Class1 = New Class1()

Either method is correct and comes down to personal preference. Depending on how many times you will be referencing the class, you may want to include the namespace instead of attaching it to the class name every time it is needed. If there were another namespace called Namespace2 nested inside of Namespace1 with the Class1 class located inside of it, then you would reference the class with Namespace1.Namespace2.Class1.

We chose Server Intellect for its dedicated servers, for our web hosting. They have managed to handle virtually everything for us, from start to finish. And their customer service is stellar.

Now we can test out our new class by adding some code to it. We are going to create a public sub called MsgBox2. This sub is going to send a message box to the user. Here is an example.

Imports Microsoft.VisualBasic

Namespace Namespace1
     Public Class Class1
           Public Sub MsgBox2()
                 MsgBox("You have accessed the Namespace correctly.")
           End Sub
     End Class
End Namespace

In order to access the sub we will need to import the namespace. Next I have declared the theClass variable as class Class1 inside of the Page_Load sub. Then we will call the MsgBox2 sub so that the user will be sent a message box when the page is first loaded. Below is an example.

Imports Namespace1

Partial Class _Default Inherits System.Web.UI.Page
     Protected Sub Page_Load(ByVal Src As Object, ByVal E As EventArgs) Handles MyBase.Load
           Dim theClass = New Class1
           theClass.MsgBox2()
     End Sub
End Class

We migrated our web sites to Server Intellect over one weekend and the setup was so smooth that we were up and running right away. They assisted us with everything we needed to do for all of our applications. With Server Intellect's help, we were able to avoid any headaches!

Namespaces can hold several different elements such as classes, structures, interfaces, enumerations, and other namespaces. These will all be referenced with the same format as directed above.

Looking for more .NET Tutorials? Click Here!

Download Project Source - Enter your Email to be emailed a link to download the Full Source Project used in this Tutorial!



100% SPAM FREE! We will never sell or rent your email address!
 
123 ASP

411 ASP

Dot Net Freaks

Server Intellect