Page=3,4,5
What we mean is that instead of having to write code that worked more or less
like this:
Select Case kindOfEmployee
Case Secretary
RaiseSalary 5%
Case Manager
RaiseSalary 10%
Case Programmer
RaiseSalary 15%
Case Architect
RaiseSalary 20%
'etc
End Select
which was a pain to maintain because whenever you added a new type of employee
you had to change all the corresponding Select Case statements, the compiler
could do the work for you. This was finally possible because starting with VB5,
you could use the magic of interface polymorphism (see Chapter 5 for more on
this) and write code like this:
For Each employee in Employees
employee.RaiseSalary
Next
and know that the compiler would look inside your objects to find the right
RaiseSalary method.
Classes let you create VB apps in a much more efficient and maintainable
manner. Whether you stick with VB5 or shift to VB .NET we cannot imagine
writing a serious VB app without them.
The .NET Mentality Shift
What does all of this have to do with .NET? Quite a lot. You see, .NET is going to
change the way you design your applications as much as the introduction of
classes to VB changed the best way to build your VB5 or 6 applications. And just as
we VB programmers suffered through the change from the classless to classenabled
incarnations of VB, so will we feel some pain in the transition to .NET!4
4. There is a conversion tool supplied with VB .NET, but we guarantee it will not ease the pain much.
Any serious program will not convert well—you’re better off redoing them from scratch.
Chapter 1
4
With that in mind, let us look at some of the things to watch out for—or take
advantage of—when switching from VB6 to VB .NET.
The Common Language Runtime
Visual Basic has always used a runtime, so it may seem strange to say that the
biggest change to VB that comes with .NET is the change to a Common Language
Runtime (CLR) shared by all .NET languages. The reason is that while on the surface
the CLR is a runtime library just like the C Runtime library, MSVCRTXX.DLL,
or the VB Runtime library, MSVBVMXX.DLL, it is much larger and has greater
functionality. Because of its richness, writing programs that take full advantage of
the CLR often seems like you are writing for a whole new operating system API.5
Since all languages that are .NET-compliant use the same CLR, there is no
need for a language-specific runtime. What is more, code that is CLR can be written
in any language and still be used equally well by all .NET CLR-compliant languages.6
Your VB code can be used by C# programmers and vice versa with no extra work.
Next, there is a common file format for .NET executable code, called Microsoft
Intermediate Language (MSIL, or just IL). MSIL is a semicompiled language that
gets compiled into native code by the .NET runtime at execution time. This is a
vast extension of what existed in all versions of VB prior to version 5. VB apps used
to be compiled to p-code (or pseudo code, a machine language for a hypothetical
machine), which was an intermediate representation of the final executable code.
The various VB runtime engines, interpreted the p-code when a user ran the
program. People always complained that VB was too slow because of this,7 and
therefore, constantly begged Microsoft to add native compilation to VB. This
happened starting in version 5, when you had a choice of p-code (small) or
native code (bigger but presumably faster). The key point is that .NET languages
combine the best features of a p-code language with the best features of compiled
languages. By having all languages write to MSIL, a kind of p-code, and then
compile the resulting MSIL to native code, it makes it relatively easy to have
cross-language compatibility. But by ultimately generating native code you still
get good performance.
5. Dan Appleman, the wizard of the VB API, intends to write a book called something like The VB
.NET Programmers Guide to Avoiding the Windows API. The .NET Framework is so fullfeatured
that you almost never need the API.
6. Thus, the main difference between .NET and Java is that with .NET you can use any language,
as long as you write it for the CLR; with Java, you can write for any platform (theoretically at
least—in practice there are some problems) as long as you write in Java. We think .NET will be
successful precisely because it leverages existing language skills.
7. Actually, this was not the bottleneck in a lot of cases. People can only click so fast and
compiled code was irrelevant in most UI situations.
Introduction
5
Completely Object Oriented
The object-oriented features in VB5 and VB6 were (to be polite) somewhat limited.
One key issue was that these versions of VB could not automatically initialize the
data inside a class when creating an instance of a class. This led to classes being
created in an indeterminate (potentially buggy) state and required the programmer
to exercise extra care when using objects. To resolve this, VB .NET adds an important
feature called parameterized constructors (see Chapter 4).
Another problem was the lack of true inheritance. (We cover inheritance in
Chapter 5.8) Inheritance is a form of code reuse where you use certain objects that
are really more specialized versions of existing objects. Inheritance is thus the
perfect tool when building something like a better textbox based on an existing
textbox. In VB5 and 6 you did not have inheritance, so you had to rely on a fairly
cumbersome wizard to help make the process of building a better textbox tolerable.
As another example of when inheritance should be used is if you want to
build a special-purpose collection class. In VB5 or 6, if you wanted to build one
that held only strings, you had to add a private instance field that you used for
the delegation process:
Private mCollection As Collection 'for delegation
Then you had to have Initialize and Terminate events to set up and reclaim the
memory used for the private collection to which you delegated the work. Next,
you needed to write the delegation code for the various members of the specialized
collection that you wanted to expose to the outside world. For example:
Sub Add(Item As String)
mCollection.Add Item
End Sub
This code shows delegation at work; we delegated the Add method to the private
collection that we used as an instance field.
The sticky part came when you wanted a For-Each. To do this you had to add
the following code to the class module:
Public Function NewEnum As IUnknown
Set NewEnum = mCollection.[_NewEnum]
End Function
and then you needed to set the Procedure ID for this code to be –4!
8. Inheritance is useful , but you should know that this is not the be-all, end-all of object-oriented
programming, as some people would have you believe. It is a major improvement in VB .NET
but not the major improvement.
No comments:
Post a Comment
This is Good Computer Information Blog.they will give
best information about computer.