Visual Basic Applications VBA Code Examples (De)Coding Ascii. All File Operations. Altering application properties from VB using Automation. Binary Conversions. Bringing an application to the foreground and altering it's window state. Convert Long Integer to an equivalent Base32 String. Visual Basic for Applications (VBA) is part of Microsoft Corporation's (NASDAQ: MSFT) legacy software, Visual Basic, which the company built to help write programs for the Windows operating system.
The first program to write is the same for all languages: Print thewords hello, world
—Brian W. Kernighan and Dennis M. Ritchie, The C Programming Language
It has become a tradition for programming books to begin with ahello, world example. The idea is that enteringand running a program—any program—may be the biggesthurdle faced by experienced programmers approaching a new platform orlanguage. Without overcoming this hurdle, nothing else can follow.This chapter contains three such examples: one that creates a consoleapplication, one that creates a GUI application, and one that createsa browser-based application. Each example stands alone and can be runas is. The console and GUI applications can both be compiled from thecommand line (yes, Visual Basic .NET has a command-line compiler!).The browser-based application requires a computer running InternetInformation Server (IIS).
This is the world’s favorite programmingexample, translated to Visual Basic .NET:
This version of hello, world is aconsole application --itdisplays its output in a Windows command-prompt window. To compilethis program, enter it using any text editor, such as Windows’sNotepad, save it in a file whose name ends with
.vb , such as Hello.vb , andcompile it from the Windows command line with this command:
The command
vbc invokes the Visual Basic .NETcommand-line compiler, which ships with the .NET Framework SDK, andinstructs it to compile the file named in the command-line argument.Compiling Hello.vb generates the fileHello.exe . After compiling, typeHello at the command line to run your program.Figure 1-1 shows the results of compiling andrunning this program.
Figure 1-1. Compiling and running hello, world
If you’re accustomed to programming in Visual Basic 6, you cansee even from this little program that Visual Basic has changeddramatically. Here’s a breakdown of what’s happening inthis code.
The first line:
indicates that the program may use one or more types defined in theSystemnamespace.(Types are grouped into namespaces to help avoid name collisions andto group related types together.) Specifically, the hello, world program uses the Console class, which is defined inthe System namespace. The
Imports statement ismerely a convenience. It is not needed if the developer is willing toqualify type names with their namespace names. For example, thehello, world program could have been writtenthis way:
However, it is customary to use the
Imports statement to reduce keystrokes and visual clutter.
An important namespace for Visual Basic developers isMicrosoft.VisualBasic. The types in this namespace expose membersthat form Visual Basic’s intrinsic functions and subroutines.For example, the Visual Basic
Trim function is amember of the Microsoft.VisualBasic.Strings class, while theMsgBox function is a member of theMicrosoft.VisualBasic.Interaction class. In addition, VisualBasic’s intrinsic constants come from enumerations within thisnamespace. Much of the functionality available in this namespace,however, is also duplicated within the .NET Framework’s BaseClass Library. Developers who are not familiar with Visual Basic 6will likely choose to ignore this namespace, favoring thefunctionality provided by the .NET Framework. The .NET Framework isintroduced later in this chapter and is explained in detail in Chapter 3.
Next, consider this line:
This line begins the declaration of a standard module named
Hello . The standard-module declaration ends withthis line:
In Visual Basic 6, various program objects were defined by placingsource code in files having various filename extensions. For example,code that defined classes was placed in
.cls files, code that defined standard modules was placed in.bas files, and so on. In Visual Basic .NET, allsource files have .vb filename extensions, andprogram objects are defined with explicit syntax. For example,classes are defined with the Class..End Class construct, and standard modules are defined with theModule..End Module construct. Any particular.vb file can contain as many of thesedeclarations as desired.
The purpose of standard modules in Visual Basic 6 was to hold codethat was outside of any class definition. For example, globalconstants, global variables, and procedure libraries were oftenplaced in standard modules. Standard modules in Visual Basic .NETserve a similar purpose and can be used in much the same way.However, in Visual Basic .NET they define datatypes that cannot beinstantiated and whose members are all static. This will be discussedin more detail in Chapter 2.
The next line in the example begins the definition of a subroutinenamed
Main :
It ends with:
This syntax is similar to Visual Basic 6. The
Sub statement begins the definition of asubroutine --amethod that has no return value.
The
Main subroutine is the entry point for theapplication. When the Visual Basic .NET compiler is invoked, it looksfor a subroutine named Main in one of theclasses or standard modules exposed by the application. IfMain is declared in a class rather than in astandard module, the subroutine must be declared with theShared modifier. This modifier indicates that theclass does not need to be instantiated for the subroutine to beinvoked. In either case, the Main subroutinemust be Public . An example of enclosing theMain subroutine in a class rather than in astandard module is given at the end of this section.
Visual Basic Application Examples With Codes
If no
Main subroutine is found, or if more thanone is found, a compiler error is generated. The command-linecompiler has a switch(/main: location ) thatallows you to specify which class or standard module contains theMain subroutine that is to be used, in the casethat there is more than one.
Lastly, there’s the line that does the work:
This code invokes the Console class’s WriteLine method, whichoutputs the argument to the console. The WriteLine method is definedas a shared (also known as astatic) method. Shared methods don’trequire an object instance in order to be invoked; nonshared methodsdo. Download facebook for mobile phone java. Shared methods are invoked by qualifying them with their classname (in this case, Console).
Here is a program that uses a class instead of a standard module tohouse its
Main subroutine. Note thatMain is declared with theShared modifier. It is compiled and run in thesame way as the standard module example, and it produces the sameoutput. There is no technical reason to choose one implementationover the other.
Here’s the GUI version of
hello, world :
This is similar to the hello, world consoleapplication, but with extra stuff required since this is a GUIapplication. Two additional
Imports statements areneeded for drawing the application’s window:
Character name character level size speed alignment player deity height sq. With armor homeland gender age ft. Base speed hair sq. Eyes temp modifiers ability name strength dexterity constitution intelligence wisdom cha armor class armor class character sheet temp temp ability modifier adjustment modifier hit points wounds/current hp. Pathfinder Character Sheet PDF Editable, Printable, Fillable Pathfinder character sheet refers to a sheet that helps the user to building up the new powerful characters. In order to play the Pathfinder role-playing game, you will be in need of the Pathfinder characters that would be more powerful than the existing competitor characters. Editable Pathfinder Character Sheet - Free download as PDF File (.pdf), Text File (.txt) or read online for free. Editable Pathfinder Character Sheet. 1 month back me also asked the same question but, now i found one best blog, whch is giving all types of pathfinder character sheet and user friends( means they upload sheets by user request,we can request through the request form. Editable pathfinder character sheet pdf. Aug 09, 2019 Pathfinder Character Sheet PDF Editable, Printable, Fillable Pathfinder character sheet refers to a sheet that assists the customer to developing the new effective characters. In order to play the Pathfinder parlor game, you will require the Pathfinder personalities that would certainly be much more powerful than the existing rival personalities.
The HelloWindows class has something that Visual Basic programs havenever seen before, the
Inherits statement:
The Visual Basic .NET language has class inheritance. TheHelloWindows class inherits from the Form class, which is defined inthe System.Windows.Forms namespace. Class inheritance and the
Inherits statement are discussed in Chapter 2.
The next line declares a label control that will be used fordisplaying the text
Hello, Windows :
The Label class is defined in the System.Windows.Forms namespace.
As is the case with console applications, GUI applications must havea shared subroutine called
Main :
This Main method creates an instance of the HelloWindows class andpasses it to the Run method of the Application class (defined in theSystem.Windows.Forms namespace). The Run method takes care of thehousekeeping of setting up a Windows message loop and hooking the HelloWindows form intoit.
Next is another special method:
Like
Main , New has specialmeaning to the Visual Basic .NET compiler. Subroutines namedNew are compiled intoconstructors.A constructor is a method that has no return value (but can havearguments) and is automatically called whenever a new object of thegiven type is instantiated. Constructors are explained further inChapter 2.
Visual Basic Program Examples
The constructor in the HelloWindows class instantiates a Labelobject, sets some of its properties, sets some properties of theform, and then adds the Label object to the form’s Controlscollection. The interesting thing to note is how different this isfrom how Visual Basic 6 represented form design. In Visual Basic 6,form layout was represented by data in
.frm files. This data was not code, but rather a listing of the propertiesand values of the various elements on the form. In Visual Basic .NET,this approach is gone. Instead, Visual Basic .NET statements mustexplicitly instantiate visual objects and set their properties. Whenforms are designed in Visual Studio .NET using its drag-and-dropdesigner, Visual Studio .NET creates this code on your behalf.
The command line to compile the Hello, Windowsprogram is:
(Note that there is no break in this line.)
The command line for compiling the Hello, Windows program has more stuff in it than the one for theconsole-based hello, world program. In additionto specifying the name of the .vb file, thiscommand line uses the
/references switch tospecify three .dll s that contain theimplementations of library classes used in the program (Form, Label,Point, etc.). The hello, world consoleapplication didn’t require references when being compiledbecause all it used was the Console class, defined in the Systemnamespace. The Visual Basic .NET command-line compiler includes tworeferences implicitly: mscorlib.dll (whichcontains the System namespace) andMicrosoft.VisualBasic.dll (which contains helperclasses used for implementing some of the features of Visual Basic.NET).
Besides the
/references switch, the command linefor compiling the Hello, Windows programincludes the /target switch. The/target switch controls what kind of executablecode file is produced. The possible values of the/target switch are:
Comments are closed.
|
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |