Visual Basic .NET

VB.NET Classes and Objects

VB.NET is an Object Oriented programming language. The Objects referred to
are created from something called a Class. You’ve already used Classes
throughout this course. But we’ll now have a closer look at them.

VB.NET is an Object Oriented programming language. The Objects referred to
are created from something called a Class. You’ve already used Classes
throughout this course. But we’ll now have a closer look at them.

 

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 have 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 umbrella.

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 the 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 is 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.

In the next part, you’ll learn how to create your own Classes

Kaynak : https://www.homeandlearn.co.uk/NET/nets11p1.html ‘sitesinden alıntı

Yorum Yap