Solved

Passing a string to return a variable value

Posted on 1998-08-21
4
289 Views
Last Modified: 2010-04-01
Is there a way of passing a string into a function, comparing its value as memory and then return the value of a static variable of another whose name is the same as the string.  

I want to be able to create a general solution to the returning of such variables since there are so many of them and they be changed over time.  Otherwise you could just have a switch statement which would return a variable depending on the string input.  This would be very messy since such a long switch statement would be required.
0
Comment
Question by:zeidman
  • 2
  • 2
4 Comments
 

Author Comment

by:zeidman
ID: 1170874
Edited text of question
0
 
LVL 8

Accepted Solution

by:
Answers2000 earned 200 total points
ID: 1170875
Use a Map

CMap in MFC, one of the map classes in MFC, full answer in the comment
0
 
LVL 8

Expert Comment

by:Answers2000
ID: 1170876
Oops i meant to say there are several map classes in STL (Standard Template Library) if you're not using MFC

In MFC here's an example

CMap< CString, CString& > mapString ;

To add to the map do
mapString.SetAt( "Key", "Value" ) ;


To Lookup use something like

CString FindItem( const CString& strKey ) // returns value
{
CString strValue ;
BOOL bFound = mapString.Lookup( strKey, strValue ) ;

// it's already there
if ( bFound )
{
// the item wasn't found, handle how you want, I'll return
// "unknown" in the example
return "unknown" ;
}

return strValue ;
}


Use for example:

mapString.SetAt( "X", 1 ) ; // variable X is one
mapString.SetAt( "Y", 2 ) ; // var Y is two

CString strInputVar = ...somecode to get user input var name...

CString strValue = FindItem( strInputVar ) ;

CString strMsg ;
strMsg.Format( "Variable %s is %s", (LPCSTR)strInputVar, (LPCSTR)strValue ) ;

This should give you enough to get started,  
AfxMessageBox( strMsg ) ;


If the variable is a true C++ variable you can also, stringize the name using the preprocessor when you add it, e.g.

Add to header:

#define AddMap( _VAR_ ) \
{\
mapString.SetAt( #_VAR_, _VAR_ ) ;\
}

Use this to include your variables in the map:

AddMap( strSomeVariable ) ;


Sorry for any typos (cut'n'paste from several programs, and trying to make the names consistent) bu this should give you the idea.

One final thing to use a CString as the key in a map it must be convertable using the HashKey function to a UINT.  Basically you must override HashKey, to generate the most unique value you possibly can for a string (doesn't have to be 100% unique, but more unique means faster).  Here's a sample of a crude Hashing function:

UINT AFXAPI HashKey( CString& key )
{
      UINT ll = key.GetLength() ;

      if ( key.GetLength() > 4 )
      {
            UINT u1 = key[0] ;
            UINT u2 = key[1] ;
            UINT u3 = key[2] ;
            UINT u4 = key[3] ;

            ll ^= ( u1 << 24 ) ;
            ll ^= ( u2 << 16 ) ;
            ll ^= ( u3 << 8 ) ;
            ll ^= ( u4 ) ;
      }

      return ll ;
}

Simply include the above before declaring/using the map.


For STL the class is "map" which is essentially similar.
0
 

Author Comment

by:zeidman
ID: 1170877
This is a good solution but it still means that I must set each value of which there may be hundreds.  (don't ask why there would be so many - not my doing!!).  I am not even sure if this is possible but what I was really after was a solution where I only have to pass a string into a function and the code "searches" for the static variable which is in the other function (which just to complicate matters happens to be in another language - but we can leave that aside for the moment).

As I said I am not sure that this is possible but I would like to try!!!
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question