This blog is targeted at beginning / novice programmers for the Visual Basic (VB) language.

Wednesday, April 4, 2007

How do you use the .NET Framework?

If you are unsure of what the .NET Framework is; you can read a brief discription in our blog (located at: http://codingchat.blogspot.com/2007/04/what-is-net.html).

 

Accessing the .NET Framework from within a programming language is actually a very simple task to accomplish. The reason it is so simple is that during the development of the framework Microsoft had created what is called Namespaces.

 

Namespaces are basically the logical location of a class. Namespaces are a structured system. It all starts with 3 primary namespaces: Microsoft, System, <Your Application Name>. The namespace can be increased to include any outside classes you reference into your application ( i.e. Custom classes you previously created, 3rd party classes, etc). Namespaces are then logically grouped inside 1 of the previous primary namespaces. Example is the 'Security' class is located inside the System name space; it would be accessed by importing the System.Security namespace.

 

Microsoft has listed all of the currently supported namespaces on their MSDN website. You can view it at: http://msdn2.microsoft.com/en-us/library/ms306608(VS.80).aspx. You should periodically review the Microsoft MSDN website to ensure you are correctly importing your desired namespace. Keep in mind this is for .NET 2.0. The .NET 1.0, 1.1 and 3.0 may have different namespace locations and/or arguments used to access them.

 

This brings us to the next gap filled by Microsoft that was plagued when using the API interfaces. The developer didn't have a clear, concise method to know how to call the API correctly. In other words if you didn't have all the arguments correctly referenced then you would possibly produce errors, or worse yet, possibly not even have a working application. There are three things Microsoft did to fix this problem. First, they had made their IDE provide tooltips on what the expected arguments are. Second, they had made the namespaces available for review offline and online through usage of the MSDN website and documentation. Third, and this could be the largest benefiting factor, is that Microsoft had made it so that some classes could be overloaded.

 

What is overloading a class? How do you do it? The basic principal is that you can redefine the class and its arguments; thus 'overloading' the class. You then would call the 'overloaded' class and the MSIL would interpret what you want to use and use the appropriate class method. This will be explained in better detail in a future blog.

There is one most common practiced method; the developer will call a line of coding to initiate the desired class from the framework. In most cases this is done prior to all other lines of coding. An example in Visual Basic to initiate the SQL class would be calling the "Imports" keyword prior to all other lines of coding in the class. An example would look like this:

 

            Imports System.Data.SQL

 

                Public Class Form1

 

                'Coding utilizing the class goes here

 

                End Class

 

You will notice in this example the first line of coding uses the keyword "Imports" and then attached to the syntax is the desired class. You may be asking yourself; since ODBC is also within the System.Data class, then why not just reference the System.Data class instead. Well, the reason is that the sub-classes (i.e. SQL, ODBC) are not visible to the parent class. Another way to put it is comparing the question to reading a book. How come you can't just read the first page of a book and claim you read the whole book, after all the remaining pages are under the first page. Well, it's simple really, it's because the top pages cover the pages under it. You have to specifically tell your application that you want to use the code under the parent class.

 

Most programming languages will operate in a similar instance to initiate a class from the framework; you will want to verify with your reference material the exact statements/syntax to use in your desired programming language.

 

This will conclude how to use the .NET framework; as I have stated previously it is not intended to be a complete resource of the subject. The starting point is the MSDN website (See the links in the links sidebar of this blog). There are many books and other websites that will cover this subject in much more detail and probably provide more thorough information. The objective of this blog was to introduce you to how to use the framework and give you resources to expand your knowledge. Always keep in mind there are three ways to program:

 

  1. The most logical way (logical to you, the developer).
  2. The most commonly accepted way (typically will include a 'best practice').
  3. The practical way (this is the way you can get it done, rather good or bad practices).

 

Regardless of the method of development, regardless of the knowledge you have; always keep plugging away and always keep expanding your knowledge. The more you learn will mean the better your programs will be and the faster you can develop them. Never be afraid to ask a question to someone more experienced than you and never be afraid to try things out and see what happens (do always be careful when using any commands that will alter and/or delete files!).

 

Happy programming!
James

No comments: