Click here to Skip to main content
15,891,567 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I try use marco define like we use in C code to control the version of VB
so
in the head of form I define a marco as below

private const VERSION=1'

and in fuction of button action i use it like

#if VERSION =1 then
a();
#else
b();
#end if

but the problem is :
it alway execute b()function;
I don't know why?whether the VB can not support the marco ?
Posted

I can only geuss based on the information you have supplied. When you say Macro am I to take this as VB for applications implementation?

If the the button click event is outside the scope of where the constant is declared it will not "see" VERSION because it is a Private decalration. It will only execute the b() function. In that case, it should have generated an error, unless option explicit is not being used.

"Version" is also a value property used for database objects in VB 6 so using it as a constant variable may have undesired effects. VB 6 is well known for allowing unsafe coding practices to be used. Try renaming the constant.
 
Share this answer
 
thanks to S Houghtelin
I know you mean.I change "VERSION" to "version_def " put the marco define and the judgement in same form
but it still not work

in the head of form
private const version_def = 1


in the click event

if version_def =1 then
a();
#else
b();
#end if
 
Share this answer
 
I tried the following in both VB 6.0 and Excel VBA and it works. You did not explain clearly which platorm so I did both.

Create a form then place a command button and a textbox in that form.
I used this code in the form, no mudules.
VB
Private Const version_def = 1

'Button Click Event Handler
Private Sub Command1_Click()
    If version_def = 1 Then
        Text1.Text = "A Path Taken"
    Else
        Text1.Text = "B Path Taken"
    End If
End Sub

Need more information about what you are using:
language, application?
Which version? VB6, VBA, VB.NET?
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900