CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Nov 2005
    Posts
    95

    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?

  2. #2
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    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"
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  3. #3
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    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

  4. #4
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    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
    Last edited by dglienna; June 2nd, 2009 at 08:47 PM.
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  5. #5
    Join Date
    Jul 2006
    Location
    Germany
    Posts
    3,725

    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.

  6. #6
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: Something funny about constants

    Quote Originally Posted by dglienna View Post
    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.
    Last edited by DataMiser; June 3rd, 2009 at 11:10 PM.

  7. #7
    Join Date
    May 2009
    Location
    London
    Posts
    51

    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)

  8. #8
    Join Date
    Nov 2005
    Posts
    95

    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!

  9. #9
    Join Date
    May 2009
    Location
    London
    Posts
    51

    Thumbs up 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.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured