|
-
Apr 1st, 2000, 11:21 AM
#1
Thread Starter
Member
Hey- What i'd like to do is create a variable that can only have certain values, like for instance the vbcolor constants where if you declare a variable as one it can only be vbred or vbblue etc. Im not sure if i do this with a user defined type or class or what. Can someone please explain it? If you still dont understand what i mean then tell me and i'll try to clarify it more....
Thnx For Your Time,
CarlosTheJackal
-
Apr 2nd, 2000, 05:39 AM
#2
If your "variable" has a continuous range of values, you can do the following:
Code:
Option Explicit
Private Enum aniAnimals
[aniLow] = 3
aniDog = 3
aniCat = 4
aniMouse = 5
[anihigh] = 5
End Enum
Private Sub Text1_Change()
If Len(Trim(Text1)) > 0 Then
If Val(Text1) < aniLow Or Val(Text1) > anihigh Then
MsgBox "Please enter an animal number (a value from 3 to 5)"
Else
MsgBox "Thanks"
End If
End If
End Sub
-
Apr 2nd, 2000, 06:09 AM
#3
Frenzied Member
Use constants, not variables
If you want to emulate the vb color "variables", you simply use named constants. That's what vbRed is. When using vbRed in your program, you can use the named constant (vbRed) or the actual long value (255) or the actual hexadecimal value (&HFF)...all will give you the same results.
To create a constant, create it in the General Declarations as Private (or in a Module as Public ) like so:
Code:
'Syntax:
<Scope> Const <Name> As <Type> = <Value>
'Example:
Private Const MyName As String = "seaweed"
Here's an example using constants in a program. I basically made my own constants for colors and used them instead of VB's intrinsic color constants.
Start a new project and drop a command button on a form (keep the default name). Paste the following code into the form's code window, run the project, and click the command button.
Code:
Option Explicit
'My own color constants
Private Const MyRed As Long = 255
Private Const MyGreen As Long = 65280
Private Const MyBlue As Long = 16711680
Private Const MyYellow As Long = 65535
Private Const MyBlack As Long = 0
Private Const MyWhite As Long = 16777215
Private Const MyMagenta As Long = 16711935
Private Const MyCyan As Long = 16776960
Private Sub Command1_Click()
Static ClickNumber As Integer
'Keep ClickNumber in the range 0 to 7
If ClickNumber = 8 Then ClickNumber = 0
Select Case ClickNumber
Case 0
Form1.BackColor = MyRed
Case 1
Form1.BackColor = MyGreen
Case 2
Form1.BackColor = MyBlue
Case 3
Form1.BackColor = MyYellow
Case 4
Form1.BackColor = MyBlack
Case 5
Form1.BackColor = MyWhite
Case 6
Form1.BackColor = MyMagenta
Case 7
Form1.BackColor = MyCyan
End Select
'Increment ClickNumber
ClickNumber = ClickNumber + 1
End Sub
-
Apr 3rd, 2000, 03:08 AM
#4
Thread Starter
Member
Thnx!
Hey- Martin, thnx that was exactly what i wanted! Seaweed, sorry i guess i worded that question badly but thanks anyway for the effort! Thanks again to you both 
Thnx For Your Time,
CarlosTheJackal
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|