010 014




Visual Basic 6 Black Book:Visual Basic Overview
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 Black Book (Publisher: The Coriolis Group) Author(s): Steven Holzner ISBN: 1576102831 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




Modules
Modules are collections of code and data that function something like objects in object-oriented programming (OOP), but without defining OOP characteristics like inheritance, polymorphism, and so on. The point behind modules is to enclose procedures and data in a way that hides them from the rest of the program. We’ll discuss the importance of doing this later in this chapter when we cover Visual Basic programming techniques and style; breaking a large program into smaller, self-contained modules can be invaluable for creating and maintaining code.

You can think of well-designed modules conceptually as programming objects; for example, you might have a module that handles screen display that includes a dozen internal (unseen by the rest of the program) procedures and one or two procedures accessible to the rest of the program. In this way, the rest of the program only has to deal with one or two procedures, not a dozen.
Besides modules, Visual Basic also supports class modules, which we’ll see later in this book when we discuss how to create ActiveX components in Chapter 20. Programming with class modules will bring us much closer to true OOP programming.
Global Items
Global items are accessible to all modules and forms in a project, and you declare them with the Public keyword. However, Microsoft recommends that you keep the number of global items to an absolute minimum and, in fact, suggests their use only when you need to communicate between forms. One reason to avoid global variables is their accessibility from anywhere in the program; while you’re working with a global variable in one part of a program, another part of the program might be busy changing that variable, giving you unpredictable results.
Now that we’ve gotten an overview of the major parts of a project, we’ll take a look at how the parts of a project interact, which brings up the idea of scope, or visibility in a project.
Project Scope
An object’s scope indicates how much visibility it has throughout the project—in the procedure where it’s declared, throughout a form or module, or global scope (which means it’s accessible everywhere). There are two types of scope in Visual Basic projects: variable scope (including object variables) and procedure scope. We’ll take a look at both of them here as we continue our overview of Visual Basic projects and how the parts of those projects interact.

Variable Scope
You declare variables in a number of ways. Most often, you use the Dim statement to declare a variable. If you do not specify the variable type when you use Dim, it creates a variant, which can operate as any variable type. You can specify the variable type using the As keyword like this:


Dim IntegerValue As Integer


Besides Dim, you can also use ReDim to redimension space for dynamic arrays, Private to restrict it to a module or form, Public to make it global—that is, accessible to all modules or forms—or Static to make sure its value doesn’t change between procedure calls. These ways of declaring variables are summarized in Table 1.2.
Table 1.2 Visual Basic declaring statements.

Keyword
Does This

Dim
Using Dim alone creates variants. Use the As keyword to specify variable type.

Private
Makes variable available only in the current form/module.

Public
Makes variable global—variable is available to the rest of program.

ReDim
Reallocates storage space for dynamic array variables.

Static
Variable preserves its value between procedure calls.

Type
Declares a user type.

There are three levels of variable scope in Visual Basic: at the procedure level, at the form or module level, and at the global level. Schematically, Figure 1.8 shows how project scope works.


Figure 1.8  Schematic of Visual Basic project scope.
When you’re designing your program, Microsoft suggests you limit your variables to the minimum possible scope in order to make things simpler and to avoid conflicts. Next, we’ll take a look at the other type of scope: procedure scope.

Procedure Scope
As with variables, you can restrict the scope of procedures, and you do that with the Private, Public, Friend, and Static keywords. The Private and Public keywords are the main keywords here; using them, you can specify if a subroutine or function is private to the module or form in which it is declared or public (that is, global) to all forms and modules. You use these keywords before the Sub or Function keywords like this:


Private Function Returns7()
Dim Retval
Retval = 7
Returns7 = Retval
End Function


You can also declare procedures as friend procedures with the Friend keyword. Friend procedures are usually used in class modules (they are not available in standard modules, although you can declare them in forms) to declare that the procedure is available outside the class, but not outside the current project. This restricts those functions from being called if the current project serves as an OLE automation server, for example.
Besides the earlier declarations, you can also declare procedures as Static, which means that the variables in the procedure do not change between procedure calls, and that can be very useful in cases like this, where we support a counter variable that is incremented each time a function is called:


Static Function Counter()
Dim CounterValue as Integer
CounterValue = CounterValue + 1
Counter = CounterValue
End Sub


That completes our overview of projects in memory now—we’ve seen how such projects are organized, what parts they have, and what scope their parts have. We’ll take a look at storing projects on disk next.

Projects On Disk
Now that we’ve created our first project—the tic-tac-toe project—we’ll save it to disk. Turn to Visual Basic now and select the Save Project As item in the Visual Basic File menu to save our new project to disk.

Visual Basic first saves the files associated with the project, and places a Save File As dialog box on the screen to save the program’s form, which Visual Basic gives the default name of Form1.frm. Change that name to tictactoe.frm now, and save it to disk (in this book, we’ll save projects in the C:\vbbb directory, so this project will go into the C:\vbbb\tictactoe directory).



Previous
Table of Contents
Next






Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited.



Wyszukiwarka

Podobne podstrony:
010 06
005 010
014 14 (7)
010 18
drugs for youth via internet and the example of mephedrone tox lett 2011 j toxlet 2010 12 014
I E 014

więcej podobnych podstron