VB.NET Classes and Objects

advertisement
VB.NET Classes and Objects
VB.NET is an Object Oriented programming language. The Objects referred to be
created from something called a Class. Class create object and object called the
method that are used in your program or calling methods.
Object oriented programming
The modern trend in programming languages is for code to be separated into
chunks. When it's being used, each chunk of code (a chunk of code that opens text
files, for example) is known as an Object. The code for the Object can then be
reused whenever it is needed. Languages like C++ and Java are Object Oriented
languages. Until Microsoft came out with VB.NET, the Visual Basic programming
language was not OOP (object oriented programming). This time it is.
Object oriented programming has a steeper learning curve, and things like
Encapsulation, Inheritance and Polymorphism have to be digested. We're not going
quite that far in this beginner's course. But you should have a good, basic
understanding of just what object are by the end of this section, and how to create
your own Objects.
Classes and Objects
In VB.NET, a class is that chunk of code mentioned earlier. You've been using
Classes all the time during this course. The Form you've started out with is a Class.
If you look right at the top of the code window for a Form, you'll see:
Public Class Form1
The word "Public" means that other code can see it. Form1 is the name of the Class
If you look at the bottom of the coding window, you'll see End Class, signifying the
end of the code for the Class.
When you place a Button or a textbox on the Form, you're really adding it to the
Form Class.
When you start the Form, VB does something called instantiation. This basically
means that your Form is being turned into an Object, and all the things needed for
the creation of the Form are being set up for you (Your controls are being added,
variables are being set up an initialised, etc.).
And that's the basic difference between a Class and an Object: A Class is the code
itself; the code becomes an Object when you start using it.
The NET Framework
The NET Framework is something that Microsoft has invested a lot of time, effort
and money into creating. It's big. Very big. The way that programming will be done
on a Microsoft machine from now on is with NET. And not just on a Microsoft
machine. There's something called ADO.NET which is used for creating web site,
and for manipulating databases. You can create applications for mobile phones and
PDA's with NET. There is even a project in the making that will allow you to write a
programme on a Windows machine that will then work on a computer NOT
running Windows. All this is made possible with the NET Framework. But what is
it?
The NET Framework is a whole lot of Classes (called Namespaces) and the
technology to get those Classes to work. The main component is called the
Common Language Runtime. A Runtime is the thing that gets your code to actually
run on a computer. Previously, these Runtime Languages were machine or
programming language specific. The Runtime that gets a Java programme to work,
for example, is different to the one that gets a C programme to work. With NET,
more than 15 different programming languages can use the Common Language
Runtime. One of these languages is, of course Visual Basic NET. Another is C#
(pronounce C Sharp). They can all use the Common Language Runtime because of
something called the Intermediate Language. (This is a sort of translator for the
various languages, and is too advanced to go into for us.)
Namespaces
A Namespace is a group of Classes which are grouped together. The System.IO
Namespace you met earlier groups together Classes that you use to read and write
to a file. System.Windows.Forms is another Namespace you've met. In fact, you
couldn't create your forms without this Namespace. But again, it is just a group of
Classes huddling under the same sunshade.
System itself is a Namespace. It's a top-level Namespace. Think of it as the leader of
a hierarchy. IO and Windows would be part of this hierarchy, just underneath the
leader. Each subsequent group of Classes is subordinate to the one they came
before it. For example Forms is a group of Classes available to Windows, just as
Windows is a group of Classes available to System. A single form is a Class available
to Forms:
System.Windows.Forms.Form
The dot notation is used to separate each group of Classes. A Button is also part of
the Forms Class:
System.Windows.Forms.Button
As too is a Textbox:
System.Windows.Forms.TextBox
The leader of the hierarchy is still System, though. Think of it as an army. You'd
have a Private who is subordinate to a Sergeant. The Sergeant would be
subordinate to a Captain. And the Captain would be subordinate to a General. If the
General wanted something done, he might ask the Captain to do it for him. The
Captain would get the Sergeant to do it, and the Sergeant would then pick on a
poor Private. So Button would be the Private, Forms would be the Sergeant,
Windows would be the Captain, and System the General.
In other words, there is a chain of command in NET programming. And if you don't
follow the chain of command, you're in trouble!
But you see this chain of command every time you type a full stop and a pop up box
appears. When you're selecting an item from the list, you're selecting the next in
the chain of command.
This code at the top of the form window:
Inherits System.Windows.Forms.Form
Means you don't have to keep typing the full chain of command every time you
want to access a button on the form. This chain of command is inherited whenever
you create a new VB.NET Form. There are plenty of times when a chain of
command is not inherited though, and in that case you do have to type it all out.
You did this when you referenced a StreamReader with:
System.IO.StreamReader
The IO Namespace is not inherited when you created a new Form, so you have to
tell VB where it is in the hierarchy.
But that's not quite the end of the story. The reason you using all of these long
Namespaces are to get at a Property or Method - the chaps that do all the actual
work! When you type Button1.Text = "Click Me", Text is a Property of Button1.
Button belongs to Form, which belongs to Forms, which belongs to Windows … etc.
So whenever you access a Property or Method of a control, just remember that
rather long chain of command.
VB.NET Class
Example
First this program introduces an Example Class. In the Class, we have a Private
Field of type Integer. We also have a constructor—the New() Sub. And finally we
have the Value() Function, which returns an expression computed based on the
_value field.
In the Main entry point, an instance of Example is created and Value() is invoked.
An Example now exists on the managed heap.
Program that uses Class [VB.NET]
Class Example
Private _value As Integer
Public Sub New()
_value = 2
End Sub
Public Function Value() As Integer
Return _value * 2
End Function
End Class
Module Module1
Sub Main()
Dim x As Example = New Example()
Console.WriteLine(x.Value())
End Sub
End Module
Output
4
Note: Most members of a Class should be Private—this improves information
hiding, which leads to higher software quality. New() Subs and certain Functions
should be Public.
Inherits
To continue, a Class can inherit from another. With Inherits, one class can inherit
from another class. This means it gains all the fields and procedures from the
parent class.
Let's beginby looking at Class A: this class contains a field (_value) as well as a Sub
(Display()). Next, Class B and Class C both use the Inherits keyword and are
derived from Class A. They provide their own constructors (New).
Program that uses inherits keyword [VB.NET]
Class A
Public _value As Integer
Public Sub Display()
Console.WriteLine(_value)
End Sub
End Class
Class B:Inherits A
Public Sub New(ByVal value As Integer)
MyBase._value = value
End Sub
End Class
Class C :Inherits A
Public Sub New(ByVal value As Integer)
MyBase._value = value * 2
End Sub
End Class
Module Module1
Sub Main()
Dim b As B = New B(5)
b.Display()
Dim c As C = New C(5)
c.Display()
End Sub
End Module
Output
5
10
Error Handling in .Net with Example
Error Handling in .Net with Easy Example and Step by Step guide.
What is Exception?
An exception is unexpected error or problem.
What is Exception Handling?
Method to handle error and solution to recover from it, so that program works
smoothly.
What is try Block?



try Block consist of code that might generate error.
try Block must be associated with one or more catch block or by finally block.
try Block need not necessarily have a catch Block associated with it but in that
case it must have a finally Block associate with it.
Example of try Block
Format1
try
{
//Code that might generate error
}
catch(Exception errror)
{
}
Format2
try
{
//Code that might generate error
}
catch(ExceptionAerrror)
{
}
catch(ExceptionB error)
{
}
Format3
try
{
//Code that might generate error
}
finally
{
}
What is catch Block?


Catch Block is used to recover from error generated in try Block.
In case of multiple catch Block, only the first matching catch Block is executed.


When you write multiple catch blocks you need to arrange them from specific
exception type to more generic type.
When no matching catch block is able to handle exception, the default behaviour
of web page is to terminate the processing of the web page.
Example of catch Block
Format1
try
{
//Code that might generate error
}
catch(Exception errror)
{
//Code that handle errors occurred in try block
}
Format2
try
{
//Code that might generate error
}
catch(DivideByZeroExceptionerrror)
{
//Code that handle errors occurred in try block
//Note: It is most specific error we are trying to catch
}
catch(Exception errror)
{
//Code that handle errors occurred in try block
//Note: It is not specific error in hierarchy
}
catch
{
//Code that handle errors occurred in try block
//Note: It is least specific error in hierarchy
}
Explain finally Block?


finally Block contains the code that always executes, whether or not any
exception occurs.
When to use finally Block? - You should use finally block to write cleanup code.
I.e. you can write code to close files, database connections, etc.




Only one finally block is associated with try block.
finally block must appear after all the catch block.
If there is a transfer control statement such as goto, break or continue in either
try or catch block the transfer happens only after the code in the finally block is
executed.
If you use transfer control statement in finally block, you will receive compile
time error.
Example of finally Block
Format1
try
{
//Code that might generate error
}
catch(Exception errror)
{
//Code that handles error and have solution to recover from it.
}
finally
{
//Code to dispose all allocated resources.
}
Format2
try
{
//Code that might generate error
}
finally
{
//Code to dispose all allocated resources.
}
Explain throw statement?



A throw statement is used to generate exception explicitly.
Avoid using throw statement as it degrades the speed.
throw statement is generally used in recording error in event log or sending an
email notification about the error.
Example of throw statement
try
{
//Code that might generate error
}
catch(Exception errror)
{
//Code that handles error and have solution to recover from it.
throw; //Rethrow of exception to Add exception details in event log or sending
email.
}
Explain using statement?


using statement is used similar to finally block i.e. to dispose the object.
using statement declares that you are using a disposable object for a short period
of time. As soon as the using block ends, the CLR release the corresponding object
immediately by calling its dispose() method.
Example of using statement in C#
//Write code to allocate some resource
//List the allocated resource in a comma-seperated list inside
//the parantheses of the using block
using(...)
{
//use the allocated resource.
}
//here dispose method is called for the entire object referenced without writing
any additional code.
Is it a best practice to handle every error?
No, it is not best practice to handle every error. It degrades the performance.
You should use Error Handling in any of following situation otherwise try to avoid
it.



If you can able to recover error in the catch block
To write clean-up code that must execute even if an exception occur
To record the exception in event log or sending email.
Difference between catch(Exception ex) and catch
try
{
}
catch(Exception ex)
{
//Catches all cls-compliant exceptions
}
catch
{
//Catches all other exception including the non-clscompliant exceptions.
}
//VB.NET: Exception Handling
1.
Module exceptionProg
Sub division(ByVal num1 As Integer, ByVal num2 As Integer)
Dim result As Integer
Try
result = num1 \ num2
Catch e As DivideByZeroException
Console.WriteLine("Exception caught: {0}", e)
Finally
Console.WriteLine("Result: {0}", result)
End Try
End Sub
Sub Main()
division(25, 0)
Console.ReadKey()
End Sub
End Module
2. Imports System
ClassMyClient
Public Shared Sub Main()
Dim x AsInteger = 0
Dim div AsInteger = 0
Try
div = 100 / x
Console.WriteLine("Not executed line")
Catch de As DivideByZeroException
‘MessageBox.Show(de.Message)
Console.WriteLine("Exception occured")
Finally
Console.WriteLine("Finally Block")
EndTry
Console.WriteLine("Result is {0}", div)
EndSub'Main
EndClass'MyClient
Download