1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

C/C++ using #define values...

Discussion in 'Software Development' started by MacFromOK, Jan 13, 2003.

Thread Status:
Not open for further replies.
Advertisement
  1. MacFromOK

    MacFromOK Thread Starter

    Joined:
    Mar 18, 2001
    Messages:
    1,741
    Hiya, :)

    I'm trying to send some window messages from user input.
    For example: BM_CLICK

    Now I know BM_CLICK is defined, but I can't figure a way
    to use the input string "BM_CLICK" and get it's defined
    value (or any other #define) without using something
    like if(lstrcmpi, Arg, "BM_CLICK" == 0) and manually
    assigning the value, or hardcoding BM_CLICK in
    SendMessage.

    Any ideas on how to convert a "BM_CLICK" string to a
    BM_CLICK message without using IF?

    Thanks, Mac :)
     
  2. codejockey

    codejockey

    Joined:
    Feb 11, 2002
    Messages:
    1,405
    You're quite right -- you can't really use BM_CLICK or any other #define at run-time unless you refer to it directly, because a #define is just a text substitution as far as the compiler is concerned (i.e., it happens at compile time, and any knowledge about the #define is lost). I've used a table and a lookup function to map the input text ("BM_CLICK") to the corresponding value; it's not particularly elegant, but it solves the problem.

    Hope this helps.
     
  3. jdean

    jdean

    Joined:
    Jan 20, 2002
    Messages:
    433
    There isn't an automatic way to do this. I would suggest creating a table of strings and values, and then scanning the table for the specified string. You'll still need an 'if', but you'll be doing this inside a loop. To add new values, you just update the table, not the code.

    You could use a hash map (if your library supports it), database or some other mechanism, but someone, somewhere, is going to be doing the equivalent of what I just described (although they might use an index or hash, instead of scanning), and you will still need to create and maintain the table.
     
  4. MacFromOK

    MacFromOK Thread Starter

    Joined:
    Mar 18, 2001
    Messages:
    1,741
    Thanks guys, :)

    Was afraid of that. I'll prolly use an array if I don't hardcode
    in the values, or I could just furnish the user with a table of
    #define numbers... ;)

    Thanks again, Mac :)
     
As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 733,556 other people just like you!

Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/113127

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice