Search Tools Links Login

Create your own custom parameters list


Ever wonder how to create your own custom list of parameters for one of your functions? You know, like the MsgBox options list with items like vbQuestion, vbExclamation, etc? This article shows you how. It is easier than you might think.

Original Author: Matt Roberts

Code




  
  


Creating Custom Option
Choices for Function Parameters

Note: A Microsoft Word
version of this article is available in .zip format below with full graphics
included. I recommend downloading it.


One of the things that I
love about Visual Basic 6 is the way it always tells you what it is expecting.
Where in most other languages, you are left guessing at the parameters
a function is expecting and what data type they should be, VB. shows you
the choices right where they are needed. For example, If I am calling the
function MsgBox to display a message box to the user, it looks like this
as I enter the code:

The usefulness of this feature
of the Visual Basic environment cannot be overstated. I often wished I
could create such option lists for my own functions. Instead of writing
a function like this:

Function
SelectCustomerCategory (CustomerType as Integer)  As String

    
Select Case CustomerType

        
Case 0


             
SelectCustomerCategory = “Corporate”


        
Case 1


             
SelectCustomerCategory = “Company”


        
Case 2


             
SelectCustomerCategory = “State Government”


        
Case 3


             
SelectCustomerCategory = “City Government”


        
Case 4


             
SelectCustomerCategory = “Federal Government”


    
End Select

End
Function

I wanted to write it like
this:

Function
SelectCustomerCategory (CustomerType as Integer)  As String

    
Select Case CustomerType

        
Case Corporate


             
SelectCustomerCategory = “Corporate”


        
Case Company


            
SelectCustomerCategory = “Company”


        
Case StateGovernment


             
SelectCustomerCategory = “State Government”


        
Case CityGovernment


             
SelectCustomerCategory = “City Government”


        
Case FederalGovernment


             
SelectCustomerCategory = “Federal Government”


    
End Select

End
Function


 

But in order to do this,
I found myself creating lots of constants like:

Const
Corporate = 0


Const
Company =1


Const
StateGovernment=2


Const
CityGovernment=3


Const
FederalGovernment =4

Although this worked, once
I had about 10 functions with five or six possible options, I started having
trouble remembering which constants were defined for which functions. They
would show up in the Options List if I pressed <ctrl>
<space>
, but since they were in alphabetical order, “Corporate
“was miles away from the other constant “StateGovernment”.

Looking back, this all seems
so useless, but at the time, I was very pleased with myself. Then one day
I was reading a Visual Basics Standards book and discovered the “enum”
data type.  I have used User Defined Types (see my article on it by
following the hyperlink) in QuickBasic and Visual Basic, so this seemed
vaguely familiar. After reading about enum, I was delighted. It was exactly
what I was looking for. With it you can define a set of parameters as a
single data type and then “alias” the values with more understandable names
(Like “Corporate” instead of “0”). When you select the parameter “Corporate”
from the drop-down list, the aliased value of “0” is passed to the function.
Sound cool? Read on and I will show you how do to it. It is actually very
easy.

First, you must define the
enum variable that will hold the values. In the declarations section of
your form or module, add the following code:

   
Public Enum enCustomerType


        
Corporate = 0


        
Company = 1


        
StateGovernment = 2


        
CityGovernment = 4


        
FederalGovernment = 5


   
End Enum

There are some “rules” I
need to point out about the above code.

1. Of course, the name of
your variable must be unique for the scope you are working in.


2. The Enum data type
can only accept Numerical values
. Strings are not allowed. Corporate
= “Corp” will not compile.


3. The list can be as long
as you like.


4. The values do not have
to be consecutive. They can be any numerical value.

Now for the function (This
is the fun part):

Instead of using:

   
Function SelectCustomerCategory (CustomerType as Integer)  As String

We are now going to use:

   
Function SelectCustomerCategory (CustomerType as enCustomerType) 
As String

So we are replacing the Integer
data type with the enum type we created.

Complete your function:

    Function
SelectCustomerCategory (CustomerType as enCustomerType)  As String

 Select
Case CustomerType

 Case
Corporate


     
SelectCustomerCategory = “Corporate”


 Case
Company


     
SelectCustomerCategory = “Company”


 Case
StateGovernment


     
SelectCustomerCategory = “State Government”


 Case
CityGovernment


     
SelectCustomerCategory = “City Government”


 Case
FederalGovernment


     
SelectCustomerCategory = “Federal Government”


 End
Select

End
Function

That is all there is to it!
Now try this:


 

Enter the following text
in a form or module:

SelectCustomerType
(

And watch what happens. You
should see a list of values appear like magic.

You can then select one of
you choices. When the function is called the value that you defined for
the enum item will be passed. For example, if you select Corporate, the
number 1 will be passed to the function.


 

 





Have Fun!
















About this post

Posted: 2002-06-01
By: ArchiveBot
Viewed: 89 times

Categories

Visual Basic 6

Attachments

CODE_UPLOAD70256232000.zip
Posted: 9/3/2020 3:45:00 PM
Size: 29,264 bytes


Loading Comments ...

Comments

No comments have been added for this post.

You must be logged in to make a comment.