Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

From String to Const

Status
Not open for further replies.

Kalin

Programmer
Jul 24, 2001
76
NL
Hi all,

I got a string. This string is the name of a Constant.
Now I want to convert this string to the value of the constant. Does anyone know how to accomplish this ??

Already tried Eval("string") but it doesn't work.
Grtz,

Kalin
 
Doesn't work....

I try will explain what I'm doing.

I got a string, let's say position with value "Center"
I also got a constant named Center with value 0.

Other combinations:

String Constant
"TopLeft" 1
"TopRight" 2
"Left" 3
"Right" 4
etc.

The names of the constants are the same as the values of the strings.

The problem is to get from the value "Center" to the value of the constant.
Grtz,

Kalin
 
Well, I'm willing to bet that most of your problem lies in your selection of constant names. You have a constant named Left which is the same as the function Left(), same thing for Right(), center is used as a property for objects and you could have problems there because access doesn't know whether to use the function or the constant.

So first off change your names to something else perhaps cLeft (for constantLeft), cRight, cCenter. Next, how are you declaring your constants? Normally you do this:

Const cCenter = 0
Const cLeft = 1
Const cRight = 2

Now you can refer to the constant and Access automatically knows what the value is based on the Const statement.

MyResult = cCenter

that would set MyResult = 0 in our case.

HTH Joe Miller
joe.miller@flotech.net
 
Also look into "Enum".

MichaelRed
mred@att.net

There is never time to do it right but there is always time to do it over
 
Thanks for the responses...

the constantnames I'm using aren't actually 'Center' etc,
Those were just examples for explaining my problem.

I'll try to illustrate the real problem....

Const tpCenter = 0
Const tp1SideA = 1
etc.

Sub test()

Dim v_test_string As String
Dim v_test_int As Integer

v_test_string = "tpCenter"
'v_test comes from an external file which is created by another app. So this isn't changable by me.

'Now the problem....
How to get the constantvalue tpCenter (eg. 0) from the string v_test_string into v_test_int....


Grtz,

Kalin
 
At the moderate expense of sounding repitious. ENUM

I do not explain it as the HELP system covers it well.


MichaelRed
mred@att.net

There is never time to do it right but there is always time to do it over
 
Kalin,

Michael is quite right. You've not declare Enumerated constants, just constants.

What you'd do is put this at the appropriate level of scope and create something along the lines of:

Private Enum
Const tpCenter = 0
Const tp1SideA = 1
End Enum

This is a feature of A2000, not available in 97 to the best of my knowledge.

Craig

PS Michael, read earlier no-one will employ you......they don't know what they're missing.......
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top