Solved

Passing a string to return a variable value

Posted on 1998-08-21
4
304 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
PDF library for Delphi 2 135
Path to  STL Map header file 1 77
HTTPSendRequest with WinINet delays on first call 11 78
boost::uuid crashes 17 36
In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
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.

740 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