| < Day Day Up > |
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
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
| < Day Day Up > |
| < Day Day Up > |
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
Listing 4.1 WinAPI.
|
|
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
|
|
Listing 4.2 .NET.
|
|
System.Drawing.Graphics.DrawImage
|
|
| < Day Day Up > |
| < Day Day Up > |
In this chapter, we
| < Day Day Up > |
| < Day Day Up > |
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 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
| Note |
If you don't
|
Table 5.1 details some of the variable types, the range of values they can store, and the memory that is allocated.
|
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 (
|
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
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
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 > |