Flylib.com

Books Software

 
 
 

The Foundation for .NET Development

 < Day Day Up > 


The Foundation for .NET Development

As was previously mentioned, you can think of the .NET Framework as a foundation on which you build and run applications. Having such a foundation makes it easier to build applications while using a consistent, simplified programming model. In earlier versions of VB, we spent a great deal of time using the Win32 API to do things that we could not natively access in the standard VB functions-things such as accessing Registry keys or creating irregularly shaped forms. Another option was third-party ActiveX controls that are actually very easy to use, but are often expensive for a single project.

This flexibility offered in the Win32 API was a blessing for VB programmers, allowing access to a variety of functions that were otherwise unavailable. Unfortunately, with this power also came a great deal of problems and confusion. Learning the API can be difficult because the calls are complex and, many times, the available examples were written for C++. This made calling the API very error-prone , often resulting in crashed computers and lost hours of coding.

Another problem was the deployment of these applications. After you finally managed to get the API calls working, you then had to deploy the appropriate DLL files with the correct version numbers . You can quickly see why this common framework makes sense from a development standpoint. You still have access to most of the powerful features of the API, without actually knowing how to implement them. In addition, deployment is now very simple because you don't need to concern yourself with how to handle the various DLL files.



 < Day Day Up > 
 < Day Day Up > 


API to VB .NET

Although the API has not become obsolete, you need to have a quick way to access the various classes that implement the functions you have used in the past. The following BitBlt API call details a portion of a common .NET equivalent:

BitBlt performs a bit-block transfer of the color data corresponding to a rectangle of pixels from the specified source device context into a destination device context. Here is a sample API call and a .NET equivalent:

Listing 4.1  WinAPI.

start example
Declare Function BitBlt Lib "gdi32" Alias "BitBlt" ( _ ByVal hDestDC As Long, _ ByVal x As Long, _ ByVal y As Long, _ ByVal nWidth As Long, _ ByVal nHeight As Long, _ ByVal hSrcDC As Long, _ ByVal xSrc As Long, _ ByVal ySrc As Long, _ ByVal dwRop As Long _ ) As Long
end example

Listing 4.2  .NET.

start example
System.Drawing.Graphics.DrawImage
end example



 < Day Day Up > 
 < Day Day Up > 


Summary

In this chapter, we looked at some of the basics of the CLR, CST, and the overall .NET Framework, a topic that encompasses so much information that we could devote an entire volume to its study. In Chapter 5, Introduction to the VB .NET Language, we look at some of the syntax of the VB .NET programming language and take an even greater look at the differences between VB6 and .NET.



 < Day Day Up > 
 < Day Day Up > 


Chapter 5: Introduction to the VB .NET Language

Although this book is focused on the creation of applications, it's important that you have some fundamental knowledge of the VB .NET language. This chapter introduces you to some of the basic principles. For those of you with programming experience, you can probably skim over this chapter and move on. For those without any programming experience, this chapter will help you with some of the basic concepts used in most VB applications.

Variables

Variables are used in almost every VB application. They are simply used to store data in a memory location that you can access and manipulate as needed. For example, suppose you are developing an application that adds two numbers together. When writing the code, you could temporarily store the values of the two numbers in separate variables with a third variable holding the resultant value. Instead of referring to an actual memory location, VB allows us to use a variable name that we can declare to refer to these values. Declaring a variable is as simple as creating a name along with a specific data type. When you declare a variable, Visual Basic allocates a certain amount of memory for the variable to store the data. It is the data type that determines exactly how much memory is being put aside.

Note 

If you don't specifically declare a data type, VB .NET assigns it to the Object data type. This is not a suggestion to do so; as for readability, you should always declare the data type, even if it is of type Object.

Table 5.1 details some of the variable types, the range of values they can store, and the memory that is allocated.

Table 5.1: Data types, size , and range of memory

Type

Size

Range

Boolean

4 bytes

True or False

Byte

1 byte

0-255 unsigned

Char

2 bytes

0-65,535 unsigned

Date

8 bytes

1/1/1 CE to 12/31/9999

Decimal

12 bytes

+/- 79,228,162,514,264,337,593,543,950,335

with no decimal point;

+/- 7.9228162514264337593543950335 with

28 places to the right of the decimal; smallest

nonzero number is

+/- 0.0000000000000000000000000001

Double

8 bytes

-1.79769313486231E308 to

-4.94065645841247E-324 for negative values;

4.94065645841247E-324 to 1.79769313486232E308

for positive values

Integer

4 bytes

-2,147,483,648 to 2,147,483,647

Long

8 bytes

-9,223,372,036,854,775,808 to

9,223,372,036,854,775,807

Object

4 bytes

Any object type

Short

2 bytes

-32,768 to 32,767

Single

4 bytes

-3.402823E38 to -1.401298E-45 for negative

values; 1.401298E-45 to 3.402823E38 for positive values

String

10 bytes

+ 0 to approximately 2 billion Unicode characters ( characters in string * 2)

{% if main.adsdop %}{% include 'adsenceinline.tpl' %}{% endif %}

With some of this basic information out of the way, we'll look at how variables are actually declared using the Dim keyword.

As in previous versions of Visual Basic, you use the Dim keyword. The following are some common examples of declaring variables:

Dim x as Single Dim txt as String Dim str as string Dim oObj as Object

By default, when you declare a variable in VB, it is initialized to a standard value (numeric variables are set to , strings are initialized to an empty string ' ', and object variables are initialized to nothing). In VB .NET, you can now initialize variables to something other than their defaults when you declare them. Here are a few examples of initializing variables when declaring them:

Dim x as Single = 1.5 Dim txt as String = "Bob" Dim Answer as Boolean = "True"

Variable declarations are usually very simple, although they can get a little more complicated when you invoke an object's constructors. Different constructors use different arguments to initialize the object. For example, suppose you need to initialize a string with 50 asterisks ('*'). You could manually type in 50 of them and it would work just fine, although you could also use a String variable constructor as follows :

Dim txt3 As New String("*", 50)

You can see that this is much easier than attempting to type out 50 asterisks and is also much more readable.

When multiple variables are declared on the same line, then its type is the same as the rest of the variables on the line. For example:

Dim x,y,z as Integer

This gives x , y , and z the Integer data type. You can take this a step further as well:

Dim x,y,z as Integer, a,b as String

This sets the x , y , z types to Integer and a , b types to String . In earlier versions of VB, these types of declarations could have caused some problems, so this is a welcome addition.



 < Day Day Up >