Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. [RESOLVED]using enum in #if preprocessor directive [modified]

[RESOLVED]using enum in #if preprocessor directive [modified]

Scheduled Pinned Locked Moved C / C++ / MFC
help
10 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    Priya_Sundar
    wrote on last edited by
    #1

    Hi, Based on the value 'SELECTED' I am trying to do some statements. The following code doesnt work for the two values of 'SELECTED'.

    typedef enum{
    APPLE =0,
    ORANGE =1
    }FRUITS;

    #define SELECTED APPLE

    #if SELECTED == 1
    AfxMessageBox("orange");
    #else
    AfxMessageBox("apple");
    #endif

    Kindly help.

    Priya Sundar

    modified on Tuesday, May 19, 2009 6:04 AM

    _ L 2 Replies Last reply
    0
    • P Priya_Sundar

      Hi, Based on the value 'SELECTED' I am trying to do some statements. The following code doesnt work for the two values of 'SELECTED'.

      typedef enum{
      APPLE =0,
      ORANGE =1
      }FRUITS;

      #define SELECTED APPLE

      #if SELECTED == 1
      AfxMessageBox("orange");
      #else
      AfxMessageBox("apple");
      #endif

      Kindly help.

      Priya Sundar

      modified on Tuesday, May 19, 2009 6:04 AM

      _ Offline
      _ Offline
      _Superman_
      wrote on last edited by
      #2

      #if is a pre-processor directive processed by the preprocessor and enum and AfxMessageBox are handled by the compiler. You cannot mix them this way. What is it that you're trying to do here?

      «_Superman_» I love work. It gives me something to do between weekends.

      T 1 Reply Last reply
      0
      • P Priya_Sundar

        Hi, Based on the value 'SELECTED' I am trying to do some statements. The following code doesnt work for the two values of 'SELECTED'.

        typedef enum{
        APPLE =0,
        ORANGE =1
        }FRUITS;

        #define SELECTED APPLE

        #if SELECTED == 1
        AfxMessageBox("orange");
        #else
        AfxMessageBox("apple");
        #endif

        Kindly help.

        Priya Sundar

        modified on Tuesday, May 19, 2009 6:04 AM

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        Priya_Sundar wrote:

        #define SELECTED APPLE

        As you've heard you can't do that. In the above line APPLE is not defined so SELECTED remains undefined so you always get

        Priya_Sundar wrote:

        AfxMessageBox("apple");

        You can only use

        #define APPLE 0
        #define ORANGE 1

        P 1 Reply Last reply
        0
        • L Lost User

          Priya_Sundar wrote:

          #define SELECTED APPLE

          As you've heard you can't do that. In the above line APPLE is not defined so SELECTED remains undefined so you always get

          Priya_Sundar wrote:

          AfxMessageBox("apple");

          You can only use

          #define APPLE 0
          #define ORANGE 1

          P Offline
          P Offline
          Priya_Sundar
          wrote on last edited by
          #4

          Thanks All... by defining it as 0 and 1 its resolved.

          Priya Sundar

          1 Reply Last reply
          0
          • _ _Superman_

            #if is a pre-processor directive processed by the preprocessor and enum and AfxMessageBox are handled by the compiler. You cannot mix them this way. What is it that you're trying to do here?

            «_Superman_» I love work. It gives me something to do between weekends.

            T Offline
            T Offline
            Taran9
            wrote on last edited by
            #5

            «_Superman_» wrote:

            You cannot mix them this way.

            Tell me one thing if I replace enum with #define and instead of AfxMessageBox, I define varA, then it compiles.

            #define APPLE 0
            #define ORANGE 1
            #define SELECTED APPLE

            #if SELECTED == 1
            int varA = 100;
            #else
            int varA = 101;
            #endif

            Replacing enum with #define is fine but isn't the definition of varA also handled by compiler only? I mean why int varA = 101; is ok and AfxMessageBox not?

            _ R 2 Replies Last reply
            0
            • T Taran9

              «_Superman_» wrote:

              You cannot mix them this way.

              Tell me one thing if I replace enum with #define and instead of AfxMessageBox, I define varA, then it compiles.

              #define APPLE 0
              #define ORANGE 1
              #define SELECTED APPLE

              #if SELECTED == 1
              int varA = 100;
              #else
              int varA = 101;
              #endif

              Replacing enum with #define is fine but isn't the definition of varA also handled by compiler only? I mean why int varA = 101; is ok and AfxMessageBox not?

              _ Offline
              _ Offline
              _Superman_
              wrote on last edited by
              #6

              I bet you have put that globally. I mean outside of all functions. You cannot call a function globally unless you are assigning to a variable. Try this.

              #if SELECTED == 1
              int varA = AfxMessageBox(L"Selected");
              #else
              int varA = AfxMessageBox(L"Not Selected");
              #endif

              «_Superman_» I love work. It gives me something to do between weekends.

              T 1 Reply Last reply
              0
              • T Taran9

                «_Superman_» wrote:

                You cannot mix them this way.

                Tell me one thing if I replace enum with #define and instead of AfxMessageBox, I define varA, then it compiles.

                #define APPLE 0
                #define ORANGE 1
                #define SELECTED APPLE

                #if SELECTED == 1
                int varA = 100;
                #else
                int varA = 101;
                #endif

                Replacing enum with #define is fine but isn't the definition of varA also handled by compiler only? I mean why int varA = 101; is ok and AfxMessageBox not?

                R Offline
                R Offline
                Rajesh R Subramanian
                wrote on last edited by
                #7

                #define APPLE 0
                #define ORANGE 1

                int varA = 0;

                #define APPLE_DEFINED

                #ifdef APPLE_DEFINED
                varA = 100;
                #else if ORANGE_DEFINED
                varA = 101;
                #else if PAPAYA_DEFINED
                varA = 102;
                #endif //APPLE_DEFINED

                It is a crappy thing, but it's life -^ Carlo Pallini

                1 Reply Last reply
                0
                • _ _Superman_

                  I bet you have put that globally. I mean outside of all functions. You cannot call a function globally unless you are assigning to a variable. Try this.

                  #if SELECTED == 1
                  int varA = AfxMessageBox(L"Selected");
                  #else
                  int varA = AfxMessageBox(L"Not Selected");
                  #endif

                  «_Superman_» I love work. It gives me something to do between weekends.

                  T Offline
                  T Offline
                  Taran9
                  wrote on last edited by
                  #8

                  Yeah! u r correct! But why so? I mean why is it ok if I assign it to an int and not otherwise?

                  _ 1 Reply Last reply
                  0
                  • T Taran9

                    Yeah! u r correct! But why so? I mean why is it ok if I assign it to an int and not otherwise?

                    _ Offline
                    _ Offline
                    _Superman_
                    wrote on last edited by
                    #9

                    It isn't about a function call actually. It is about initializing a global variable even if it means calling a function to do it. So we can use that trick to execute a function even before the entry point of a program.

                    «_Superman_» I love work. It gives me something to do between weekends.

                    T 1 Reply Last reply
                    0
                    • _ _Superman_

                      It isn't about a function call actually. It is about initializing a global variable even if it means calling a function to do it. So we can use that trick to execute a function even before the entry point of a program.

                      «_Superman_» I love work. It gives me something to do between weekends.

                      T Offline
                      T Offline
                      Taran9
                      wrote on last edited by
                      #10

                      ok.. got it. Thanks!

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups