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
Note: A Microsoft Word One of the things that I The usefulness of this feature Function End I wanted to write it like Function End But in order to do this, Const Although this worked, once Looking back, this all seems First, you must define the There are some “rules” I 1. Of course, the name of Now for the function (This Instead of using: We are now going to use: So we are replacing the Integer Complete your function: Function Select Case End That is all there is to it! Enter the following text SelectCustomerType And watch what happens. You You can then select one of Have Fun!
Choices for Function Parameters
version of this article is available in .zip format below with full graphics
included. I recommend downloading it.
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:
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:
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
Function
this:
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
Function
I found myself creating lots of constants like:
Corporate = 0
Const
Company =1
Const
StateGovernment=2
Const
CityGovernment=3
Const
FederalGovernment =4
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”.
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.
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
need to point out about the above code.
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.
is the fun part):
Function SelectCustomerCategory (CustomerType as Integer) As String
Function SelectCustomerCategory (CustomerType as enCustomerType)
As String
data type with the enum type we created.
SelectCustomerCategory (CustomerType as enCustomerType) As String
Case CustomerType
Corporate
SelectCustomerCategory = “Corporate”
Case
Company
SelectCustomerCategory = “Company”
Case
StateGovernment
SelectCustomerCategory = “State Government”
Case
CityGovernment
SelectCustomerCategory = “City Government”
Case
FederalGovernment
SelectCustomerCategory = “Federal Government”
End
Select
Function
Now try this:
in a form or module:
(
should see a list of values appear like magic.
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.
About this post
Posted: 2002-06-01
By: ArchiveBot
Viewed: 92 times
Categories
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.