Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Predefined values as arguement

Posted on 2002-03-24
2
Medium Priority
?
267 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 1

Accepted Solution

by:
Ryan_231 earned 800 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…
Suggested Courses

722 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