Solved

Passing a string to return a variable value

Posted on 1998-08-21
4
266 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
Comment Utility
Edited text of question
0
 
LVL 8

Accepted Solution

by:
Answers2000 earned 200 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

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…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

771 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now