Solved

Predefined values as arguement

Posted on 2002-03-24
2
261 Views
Last Modified: 2010-05-02
Here's my question...I have a function in a class that accepts a string argument.  I want to predefine the possible values that can be passed as this arguement.  Let's use colors as an example.

Public Function DoSomething(ByVal strColor As String) as String

Select Case strColor
   Case "red"
      ' Code here
   Case "blue"
      ' Code here
   Case "green"
      ' Code here
End Select

End Function

Given that code, how can I force the object calling this method to select from a list of red, blue, and green.  It seems like I should be able to create a collection of colors and declare the argument as my collection but that didn't work.  Seems like this should be easy enough.
0
Comment
Question by:jayh
2 Comments
 
LVL 1

Accepted Solution

by:
Ryan_231 earned 200 total points
ID: 6892611
use an enum

Public Enum Color
  Red = 1
  Blue = 2
  Green = 3
End Enum

then in your function

Public Funtion DoSomething(c as Color) As String
  Select Case c
    case is = Red
     'code
    case is = Blue
     'do something
  End Select

  or you can do this

  Select Case c
    case is = 1
     'code for red
    case is = 2
      'code for blue
  End Select
End function

note when you call the function you will have to do it like this

Dim myColor AS Color
myColor = Red
call DoSomething(myColor)
0
 

Author Comment

by:jayh
ID: 6893112
Thanks, I knew it was something simple but I just couldn't remember what it was.
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question