article

Create your own custom parameters list

Email
Submitted on: 1/7/2015 8:40:00 AM
By: Matthew Roberts (from psc cd)  
Level: Intermediate
User Rating: By 9 Users
Compatibility: VB 5.0, VB 6.0
Views: 858
 
     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.

This article has accompanying files

 
				






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!







winzip iconDownload article

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. Afterdownloading it, you will need a program like Winzip to decompress it.Virus note:All files are scanned once-a-day by Planet Source Code for viruses, but new viruses come out every day, so no prevention program can catch 100% of them. For your own safety, please:
  1. Re-scan downloaded files using your personal virus checker before using it.
  2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.
  3. Scan the source code with Minnow's Project Scanner

If you don't have a virus scanner, you can get one at many places on the net including:McAfee.com


Other 25 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this article (in the Intermediate category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular article, please click here instead.)
 

To post feedback, first please login.