Solved

Passing a string to return a variable value

Posted on 1998-08-21
4
312 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
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 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 learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

615 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