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. how to initialize a variable whose name is passed as a string with a given value in a function in vc++6.0

how to initialize a variable whose name is passed as a string with a given value in a function in vc++6.0

Scheduled Pinned Locked Moved C / C++ / MFC
c++helptutorial
8 Posts 6 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.
  • M Offline
    M Offline
    manoharbalu
    wrote on last edited by
    #1

    My requirement is in MFC vc6.0 application I need a function which receives a string and a float value The string that i pass is a variable name used in the application. When the function is called the passed value should get assigned to the variable which is passed as a string Function SetVal(CString VarName, float value) { Suppose is Esim->Aut[35] and is 5.5665 This function should set Esim->Aut[35] to 5.5665 } Anybody pl help if this is possible.

    C _ D M 4 Replies Last reply
    0
    • M manoharbalu

      My requirement is in MFC vc6.0 application I need a function which receives a string and a float value The string that i pass is a variable name used in the application. When the function is called the passed value should get assigned to the variable which is passed as a string Function SetVal(CString VarName, float value) { Suppose is Esim->Aut[35] and is 5.5665 This function should set Esim->Aut[35] to 5.5665 } Anybody pl help if this is possible.

      C Offline
      C Offline
      Chris Losinger
      wrote on last edited by
      #2

      C++ does not have a built-in way to associate a variable name with a string, at runtime. so, you will need to construct a way to create this association explicitly. there are many ways to do it. the most basic is something like this:

      void SetVal(CString VarName, float value)
      {
      if (VarName=="Var1")
      Var1 = value;
      else if (VarName=="Var2")
      Var2 = value;
      else if (VarName=="Variable3")
      Variable3 = value;
      etc..
      }

      image processing toolkits | batch image processing

      M 1 Reply Last reply
      0
      • M manoharbalu

        My requirement is in MFC vc6.0 application I need a function which receives a string and a float value The string that i pass is a variable name used in the application. When the function is called the passed value should get assigned to the variable which is passed as a string Function SetVal(CString VarName, float value) { Suppose is Esim->Aut[35] and is 5.5665 This function should set Esim->Aut[35] to 5.5665 } Anybody pl help if this is possible.

        _ Offline
        _ Offline
        _AnsHUMAN_
        wrote on last edited by
        #3

        I don't know of such a way but if you elaborate on your requirement someone here might help you on this. Another way would be to keep a check on the string passed and then compare it with what you want, thereafter set the value in the required value but then this may be an additional overhead depending on the parameters used in your code.

        You talk about Being HUMAN. I have it in my name AnsHUMAN

        1 Reply Last reply
        0
        • C Chris Losinger

          C++ does not have a built-in way to associate a variable name with a string, at runtime. so, you will need to construct a way to create this association explicitly. there are many ways to do it. the most basic is something like this:

          void SetVal(CString VarName, float value)
          {
          if (VarName=="Var1")
          Var1 = value;
          else if (VarName=="Var2")
          Var2 = value;
          else if (VarName=="Variable3")
          Variable3 = value;
          etc..
          }

          image processing toolkits | batch image processing

          M Offline
          M Offline
          manoharbalu
          wrote on last edited by
          #4

          Thanks for your help. But... This doesnt help me :-( Becoz I have more than 1000 variables in my application for which I have to write this code to check for the variable. Pl. suggest me if there is any direct way of doing the mapping.

          C H 2 Replies Last reply
          0
          • M manoharbalu

            Thanks for your help. But... This doesnt help me :-( Becoz I have more than 1000 variables in my application for which I have to write this code to check for the variable. Pl. suggest me if there is any direct way of doing the mapping.

            C Offline
            C Offline
            Chris Losinger
            wrote on last edited by
            #5

            manoharbalu wrote:

            Pl. suggest me if there is any direct way of doing the mapping.

            there is no direct way of doing this. C++ does not know the 'name' of your variables at runtime. if you have a lot of variables to deal with, you can use a std::map to map strings to floats. you will have to add the variable names to the map explicitly, but accessing the stored values will be simpler. Google "C++ variable name map". this is a common question, and there are a lot of solutions out there.

            image processing toolkits | batch image processing

            1 Reply Last reply
            0
            • M manoharbalu

              Thanks for your help. But... This doesnt help me :-( Becoz I have more than 1000 variables in my application for which I have to write this code to check for the variable. Pl. suggest me if there is any direct way of doing the mapping.

              H Offline
              H Offline
              Hans Dietrich
              wrote on last edited by
              #6

              manoharbalu wrote:

              Becoz I have more than 1000 variables in my application for which I have to write this code

              Write yourself a little app that will output the variable names like this:

              "variable-name", variable-name,

              Do that for all the variables, paste the output into your program, and you have a table that you can search at run-time. By the way, use of sms-speak (Becoz, pl.) is not appreciated on this site.

              Best wishes, Hans


              [Hans Dietrich Software]

              1 Reply Last reply
              0
              • M manoharbalu

                My requirement is in MFC vc6.0 application I need a function which receives a string and a float value The string that i pass is a variable name used in the application. When the function is called the passed value should get assigned to the variable which is passed as a string Function SetVal(CString VarName, float value) { Suppose is Esim->Aut[35] and is 5.5665 This function should set Esim->Aut[35] to 5.5665 } Anybody pl help if this is possible.

                D Offline
                D Offline
                David Crow
                wrote on last edited by
                #7

                manoharbalu wrote:

                This function should set Esim->Aut[35] to 5.5665

                So why not just use:

                Esim->Aut[35] = 5.5665;

                "One man's wage rise is another man's price increase." - Harold Wilson

                "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

                "Some people are making such thorough preparation for rainy days that they aren't enjoying today's sunshine." - William Feather

                1 Reply Last reply
                0
                • M manoharbalu

                  My requirement is in MFC vc6.0 application I need a function which receives a string and a float value The string that i pass is a variable name used in the application. When the function is called the passed value should get assigned to the variable which is passed as a string Function SetVal(CString VarName, float value) { Suppose is Esim->Aut[35] and is 5.5665 This function should set Esim->Aut[35] to 5.5665 } Anybody pl help if this is possible.

                  M Offline
                  M Offline
                  Mattias G
                  wrote on last edited by
                  #8

                  Have you considered using some kind of macro (using a #define) instead of a function call?

                  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