Visual Basic 6 Programming Blue Book: The Most Complete, Hands-On Resource for Writing Programs with Microsoft Visual Basic 6!:The Basics Of Basic
function GetCookie (name)
{
var arg = name + "=";
var alen = arg.length;
var clen = document.cookie.length;
var i = 0;
while (i < clen)
{
var j = i + alen;
if (document.cookie.substring(i, j) == arg) {
var end = document.cookie.indexOf (";", j);
if (end == -1)
end = document.cookie.length;
return unescape(document.cookie.substring(j, end));
}
i = document.cookie.indexOf(" ", i) + 1;
if (i == 0) break;
}
return null;
}
var m1='';
var gifstr=GetCookie("UsrType");
if((gifstr!=0 ) && (gifstr!=null)) { m2=gifstr; }
document.write(m1+m2+m3);
Keyword
Title
Author
ISBN
Publisher
Imprint
Brief
Full
Advanced Search
Search Tips
Please Select
-----------
Components
Content Mgt
Certification
Databases
Enterprise Mgt
Fun/Games
Groupware
Hardware
IBM Redbooks
Intranet Dev
Middleware
Multimedia
Networks
OS
Prod Apps
Programming
Security
UI
Web Services
Webmaster
Y2K
-----------
New Titles
-----------
Free Archive
To access the contents, click the chapter and section titles.
Visual Basic 6 Programming Blue Book: The Most Complete, Hands-On Resource for Writing Programs with Microsoft Visual Basic 6!
(Publisher: The Coriolis Group)
Author(s): Peter G. Aitken
ISBN: 1576102815
Publication Date: 08/01/98
function isIE4()
{
return( navigator.appName.indexOf("Microsoft") != -1 && (navigator.appVersion.charAt(0)=='4') );
}
function bookMarkit()
{
var url="http://www.itknowledge.com/PSUser/EWBookMarks.html?url="+window.location+"&isbn=0";
parent.location.href=url;
//var win = window.open(url,"myitk");
//if(!isIE4())
// win.focus();
}
Search this book:
Previous
Table of Contents
Next
Variable names should follow one other guideline. While it may not be an absolute requirement, it is nonetheless important enough to merit the designation of a rule: Use descriptive variable names.
This means simply that the name of a variable should describe the data it holds. For example, if your program deals with the population genetics of bats, and you need a variable to hold the number of bats, you should use a name such as Bat_Count or NumberOfBats. You could legally name the variable XY, J89, or BillClinton, and Visual Basic wouldnt care. But when working on the program later, youll find that descriptive variable names make a big difference in the readability of your code. Its a good habit to cultivate right from the get-go.
Some programmers believe that descriptive variable naming should be taken a step further so that the name of each variable identifies not only its contents but its type. This is typically done with a type of prefix called Hungarian notation. Using this prefix, the name of an integer variable might be preceded with an i for integer. For your bat count, therefore, you would have a variable named iNumberOfBats. The idea behind Hungarian notation is the ability to discern the variable type immediately whenever you see a variable name in code.
In my opinion, there is no need for the inconvenience of Hungarian notation in Visual Basic. The Visual Basic code editor already has a feature that makes it a cinch to determine the type of any variable. Simply right-click on any variable name, then select Quick Info from the pop-up menu. Visual Basic will display a small window with the variable type indicated. You can get the same result by placing the editing cursor on the variable and pressing Ctrl+I. With such easy access to variable types, why bother with the awkward and inconvenient variable names created by Hungarian notation?
TIP: Standardizing Variable Naming Conventions
Visual Basic does not care one whit about the case of variable names. As far as Basic is concerned, Count, count, and COUNT all reference the same variable. For the sake of code readability, I suggest that you adopt a standard variable naming scheme and stick to it. My method is to use a combination of upper- and lowercase letters for variable names. This approach makes the names easier to read, particularly when a variable name has been constructed by combining two or more words. For example, I think that MaxDataCount is much easier to read than maxdatacount. As Ill soon explain, I reserve names that are all uppercase for constants.
Visual Basics code editor makes it easy to be consistent with your use of capitalization. Once youve entered the first instance of a variable name, which is usually in a Dim statement, Visual Basic automatically corrects future instances of the name to match the original capitalization.
How is a variables type assigned? You have two choices. You can explicitly declare the variable in a Dim statement, or you can use a type declaration character at the end of the variable name. An explicit declaration takes the following general form:
Dim name As type
Heres a specific example:
Dim X As Integer, Y As Single, Z As Currency
Each variable name is listed, followed by the As keyword and the type name. You can have one or more variables in a single Dim statement, and you can have as many Dim statements as you need, each on its own line.
The use of type declaration characters makes Dim statements unnecessary. Ill explain in a minute why I think that the use of type declaration characters is a bad idea, but you do need to know about them. To use the type declaration characters, simply place the character corresponding to the desired variable type at the end of the variable name. The characters are shown in Table 4.2.
At this point, because no Dim statement is necessary, you simply use the variable in code as needed. For example, if you need a type Integer variable to hold the value 100, you would write as follows:
Count% = 100
In other words, you use the variables only when and where they are neededyou dont have to declare them first. This all sounds quite convenient, but now Ill explain why its a bad idea.
To Declare Or Not To Declare
The answer to this questionto declare or notis an easy one. Always declare your variables. In fact, you can tell Visual Basic to require variable declaration. Select Options from the Tools menu, click on the Editor tab, and turn on the Require Variable Declaration option. When this option is active, Visual Basic will report a Variable Not Defined error if you try to use a variable that has not been declared in a Dim statement.
Table 4.2 Type declaration characters.
Type
Declaration Character
Integer
%
Long
&
Single
!
Double
#
Currency
@
Note: When the Require Variable Declaration option is on, Visual Basic inserts the Option Explicit statement in your code.
Why am I so adamant about always declaring your variables? Why wouldnt the use of type declaration characters be easier? Though it may appear easier at first, it always seems to lead to problems. Heres why. Lets say youve created a variable, but at some point in the code the name is misspelled. Without Option Explicit active, Visual Basic will simply create a new variable using the misspelled name (as far as Visual Basic is concerned, you are creating a new variable). New variables are automatically given the value of zero. Because the program is using this new variable instead of the one it should be using, you are almost sure to get program errors that are extremely hard to track down. If Option Explicit were active, however, Visual Basic would immediately flag the misspelled variable name as an undeclared variable. The problem would never arise.
But this isnt the only reason to declare your variables. Some of Visual Basics variable typesByte, Boolean, and Date (Ill get to the last two soon)do not have a type declaration character associated with them. If you want to create a variable with one of those types, you must use a Dim statement. If you are not requiring variable declarations, youll find yourself in a situation where some of your programs variables are declared in Dim statements, while others are not. This is a recipe for confusion, and any seasoned programmer knows potential confusion should be nipped in the bud.
Previous
Table of Contents
Next
Products | Contact Us | About Us |
Wyszukiwarka
Podobne podstrony:
ZL4 04 02monter systemow rurociagowychq3[04]? 02 nAresztowano instruktorkę 80 terrorystek (04 02 2009)TI 01 04 02 B pl(2)Rząd Start w wyborach wykluczonych polityków nielegalny (04 02 2010)Warunki przystąpienia do egzaminu na uprawnienia na dzien 04 02 201104 j 02WSM 04 02 pl(1)2015 04 02 Dec nr 107 MON 16 BP odznaka pamiątkowa19 04 02 03 2011 Racjonalne podstawy skojarzonej terapii nadciśnienia tętniczego Szmelc, Szmeltewięcej podobnych podstron