
Build a Command-Line Dictionary
by Marc Mercuri
December 2002 Issue
Technology Toolbox: VB6
Level: Intermediate
Many times you'll want your applications to read in parameters from the command line. In VB6, the ability to capture the entire command line is built in with the Command variable. However, a collection containing command-line switches and their values isn't.
Building a routine to create such a collection is relatively straightforward and involves using a Dictionary object. The Dictionary object is available as part of the Microsoft Scripting Runtime (scrrun.dll). It allows you to create a collection much like an actual dictionary—you add an association between a term (referred to as a key) and its definition.
You can check command-line parameters in two ways. In some instances, you'll want to branch off your code based on the presence of a given parameter, but at that point, you're not concerned with what the value is. In that case, you could check for the presence of your command-line switch using the Exists method on the dictionary, which would return a Boolean value. If you want to evaluate the value passed in with the command-line switch, you can access the value by providing the switch name to the dictionary's item collection. The command line can include multiple switches, in any order.
Here's a sample command line:
Myapp.exe -MyParameter HelloWorld
For testing purposes, I've written code that checks to see if MyParameter is a switch on the command line in Sub Main. If MyParameter exists, the value associated with it (HelloWorld) will be displayed in a message box.
Back to top
|