Something funny about constants
I have a program where I define:
Dim strMyLetter as String
Dim strTheChar as String
strTheChar = Chr(5) ' never changes
If strMyLetter = strTheChar Then....... works fine
Since I do big loop of character checking I thought I could changeout strTheChar instead to a constant & gain some program speed:
Const strTheChar as String = &H5
If strMyLetter = strTheChar Then....... now does not seem to work ?????
Is the "&H5" proper, or is something else going wrong?
Re: Something funny about constants
Code:
Const strTheChar as String = &H5
That doesn't work because &H implies a Hexidecimal number
Code:
Const strTheChar as String = "5"
Re: Something funny about constants
Actually it would need to be .... as string=Chr(5) but this is not supported in VB6 so you must do it with a var as in the original snippet
Re: Something funny about constants
oops. missed that one.
EDIT: Although in this case they are both strings, and &H5 = "5" anyways. Chr$(47) (or whatever letter that is) works
Re: Something funny about constants
Yes, that's true. You can assign any expression consisting of other constants to a constant, but the result of a simple function which surely is meant to be constant is denied.
There is no workaround known to me.
Re: Something funny about constants
Quote:
Originally Posted by
dglienna
oops. missed that one.
EDIT: Although in this case they are both strings, and &H5 = "5" anyways. Chr$(47) (or whatever letter that is) works
In the case of the op he is referring to Chr(5) not literal "5" this is the ENQ character denoted in terminal font by a character like you would find on a club playing card.
The CHR() function as part of a constant assignment generates an error and this character is not typeable from the keyboard in VB6.
Re: Something funny about constants
Amazed that this works but here goes.
In your code add then following.
Const strTheChar = "5"
now in the immediate window type ? chr(5)
this will display a small square in the immediate window
highlight the box, right click and copy.
paste this over the 5 in the line Const strTheChar = "5" and you end up with the following
Code:
Const strTheChar = ""
You now have a constant with the value chr(5)
Re: Something funny about constants
Thanks Killa69
That's quite a trick...I wonder how you can set a constant to carriage return (hex 0D ), supposing vb did not already have vbCR defined already---I don't think your printout cut/paste would work then. But its what I need for now!
Re: Something funny about constants
Not sure but that's probably why the VBA.Constants have VbCr, VbCrLf, VbTab, VbNullChar Etc.
They seem to have covered all the ones that are likely to cause a problem.