How to return new String from a function? (SOLVED)

Started by RickJ, Wed 01/02/2006 06:26:03

Previous topic - Next topic

RickJ

I am upgrading some code to use the new string stuff but am having a small problem.  It's probaly something obvious or a misconception I have.  Perhaps someone can straighten me out.   

Basicaly I am implementing a dictionary consisting of "name" and "value" pairs. 
The function get_buf() looks up the name and returns the associated value.  The logic and code worked fine before trying to upgrade.  Now however, the same code is unable to return the value.   

With old style strings it was possible to return string values by writing to a puunctions parameters.  So do the new style strings not work this way or am I missing something else.   Thanks in advance for any enlightment anyone can provide.


This example is illustrative of the above description.
String  DataName[10];
String  DataValue[10];

function get_buf(String name, String value) {
 
   // The dictionary is filled out by reading a file.
   // in another function.  For the test we will just set it .
   DataValue[0]="555";  

   // A while loop is used to look up the name and get 
   // the corresponding value.   
   value = DataValue[0];

   // At this point "value" contains the cotrrect data
   return true;
}

iGet(String name) {
   String buf="";
   int status;

   // Call the above lookup function.  
   status = get_buf(name,buf);

   // The parameter buf does not contain any 
   // data after the call.
}



SSH

As long as you don't enforce new style strings, you can still pass a "string" rather than a "String". This will make the string argument modifiable in-place.

However, you can also declare a function that returns a String by replacing "function" (which is just #defined to "int" anyway) with String.
12

monkey0506

#2
The problem with your code is that you can't update Strings like you could strings.  But, like SSH mentioned, you can return a String (whereas although it may have worked we were told we couldn't do it with strings).  So basically you would type it like this pseudo-code:

Code: ags
String get_buf(String name) {
// alternatively "String get_buf(const string name) { " (To allow it to work with old & new style strings)
  if (name == null) return null;
  int i = 0;
  while ((i < 10) && (DataName[i] != name)) i++;
  if (i == 10) return null;
  return DataValue[i];
}

iGet(String name) {
  String buf = get_buf(name) {
  bool status = (buf == null);
}


This code would return a String from the function.  If a value was found for the NAME parameter, it would return the appropriate value, otherwise it would return null.  This value would then be stored in the local String buf of the iGet function, and status would tell whether or not the value was found.

Now your code probably won't look exactly like this, but hopefully it will help get you on the right track. ;)

RickJ

#3
SSH, Monkey,

Thanks for taking the time to reply.  Your explanations were very helpful.   I thought that this may be the case but wasn't sure.   Now that I have a better understanding I should be able to proceed without problems.   

Hehe, I guess I should have taken SSH's earlier advice and RTFM!   :=

Edit:
I made the changes and everything works great.  Also the code is much cleaner now as well.

SMF spam blocked by CleanTalk