none
To add Enums Dynamically.

    Question

  • Hi,

    I have an Enum List say Enum abcd

       {

    a = 1,

    b = 2,

    c = 3

      }

    I would like to create or add the enums dynamically. Already we have used the enum in several pojects in our solution, right now we would like to add some new item to the enum, without compiling the code or providing an installer to our customers. Is it possible to add the enums dynamically ?


    Sarvanan
    Monday, March 28, 2011 4:44 PM

Answers

  • While you would certainly need a code update to move to the new system, at the simplest level all you have to do is cast some integer value to the enum type and store that off in a variable and you have "expanded" the type.  As long as all code that uses that type will expect that there may be new values, you are fine.

    You could certainly make an extensible system that you can add new bardcode types by delivering new files to the customer with something like:

    <BarcodeType name="Type39" plugin="Type39.dll" processfunc="Type39ScanImage" />

    You could read that XML at runtime, determine which DLL to load and which function within the DLL to call.

    • Marked as answer by Rob Pan Monday, April 04, 2011 1:47 AM
    Monday, March 28, 2011 6:16 PM
  • Adding values to enum breaks binary compatibility. You need some kind of database. If you really want to add value to enum, recompile every file that use the enum.

    The following is signature, not part of post
    Please mark the post answered your question as the answer, and mark other helpful posts as helpful, so they will appear differently to other users who are visiting your thread for the same problem.
    Visual C++ MVP
    • Marked as answer by Rob Pan Monday, April 04, 2011 1:47 AM
    Monday, March 28, 2011 9:10 PM

All replies

  • enum ABCD {
     a = 1,
     b = 2,
     c = 3
    };
    
    const ABCD d = static_cast<ABCD>(4);
    
    
    
    
    

    I am not sure how you plan to do anything without compiling any code or delivering that compiled code to your customers.  Perhaps you can shed some light on things here.

    If your enum is in a header file and you don't want to recomile your entire workspace you could add a new constant, like I showed in my example.  Be forewarned though, that if you have code that is not expecting the new value, there is no telling what could happen when it gets the unexpected value.  For example code like

    if (value == a) {

    } else if (value == b) {

    } else { // must be C

    }

     

    Monday, March 28, 2011 5:06 PM
  • Thanks for your quick response, sorry if the details are not sufficient.

     

    We have delivered our software to our customers. our software scans the barcodes of different types. These barcode types are represented are enums in our code. Now our customers would like to add new barcode types, now we need to add these types in code and rebuild the installer and need to deliver. Is there any way that we could achieve adding the enums dynamically during the runtime or adding these barcode types in an xml file. so that in future if there are new barcode types in the market then they could be added in a xml file, instead we updating our code and delivering an installer to the customer.


    Sarvanan
    Monday, March 28, 2011 5:37 PM
  • While you would certainly need a code update to move to the new system, at the simplest level all you have to do is cast some integer value to the enum type and store that off in a variable and you have "expanded" the type.  As long as all code that uses that type will expect that there may be new values, you are fine.

    You could certainly make an extensible system that you can add new bardcode types by delivering new files to the customer with something like:

    <BarcodeType name="Type39" plugin="Type39.dll" processfunc="Type39ScanImage" />

    You could read that XML at runtime, determine which DLL to load and which function within the DLL to call.

    • Marked as answer by Rob Pan Monday, April 04, 2011 1:47 AM
    Monday, March 28, 2011 6:16 PM
  • Adding values to enum breaks binary compatibility. You need some kind of database. If you really want to add value to enum, recompile every file that use the enum.

    The following is signature, not part of post
    Please mark the post answered your question as the answer, and mark other helpful posts as helpful, so they will appear differently to other users who are visiting your thread for the same problem.
    Visual C++ MVP
    • Marked as answer by Rob Pan Monday, April 04, 2011 1:47 AM
    Monday, March 28, 2011 9:10 PM