Image of Navigational Map linked to Home / Contents / Search Pop(up) goes the Value!

by Rupert Walsh - GUI Computing
Image of Line Break

We all know that code reuse is a "good thing", and one of the best things about the VB editor (versions 5 and 6) it the auto-complete function that pops up syntax.

Anybody who's written a function (or sub that takes parameters) in VB5 will have seen that the parameter list pops up automatically when you call it, just like they do for the built in VB functions.

What you may not know however, is how to code functions to pop up lists of values like those popped up by MsgBox for example:

The answer is to use enumeration. Here's a function that I've created to do string comparisons, called StrCompEx. Without going into the details, it is just like the standard StrComp, but has an extra type of comparison (explained in detail in another article). I use enumeration for the comparison type, and for the result returned. The enumerations are defined in the General Declarations section of a module or class:

Option Explicit
'Compare types for StrCompEx
Public Enum eCompareType
    ctBinary = 0
    ctString = 1
    ctIndexNumeric = 2
End Enum

'Compare results for StrCompEx
Public Enum eCompareResult
    crError = -2
    crFirst = -1
    crEqual = 0
    crSecond = 1
End Enum

In the function definition, the compare parameter and the return result are declared as the enumerated types that we have already defined:

Public Function StrCompEx(ByVal sFirst As String, ByVal sSecond As String, _
    Optional ByVal nCompareType As eCompareType = ctBinary) As eCompareResult
    'Function Code goes here

End Function

You can see here, that when I call the function, not only is the parameter list displayed, but also a drop-down list with the allowed values of the enumeration:

Note that I 'dimmed' a variable nResult as the enumeration type returned by the function. When we check for the values of this variable, VB also pops up a drop-down list for us to select from:

In fact, this feature has proved so useful that Microsoft has now included it in the Visual Interdev 6 editor as well.

What all this means of course, is that you'll be able to use the functions you write more easily, over and over again. So start building up that code library!



Written by: Rupert Walsh
October '98

Image of Arrow linked to Previous Article Image of Arrow linked to Next Article
Image of Line Break
[HOME] [TABLE OF CONTENTS] [SEARCH]