Solved

Predefined values as arguement

Posted on 2002-03-24
2
259 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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

746 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now