?
Solved

Passing a string to return a variable value

Posted on 1998-08-21
4
Medium Priority
?
324 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 400 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

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

Question has a verified solution.

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

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses

601 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