Page 1 of 1

problems returning wxString

Posted: Thu Dec 08, 2011 12:57 am
by TexasJimbo
I have had problems returning a value of wxString and am wondering if there are known problems doing this, or perhaps I am not doing it correctly....In general, sometimes the value is blank even though it was set in the called routine. The following simple example is what I am talking about. There are two objects from two different classes...one calls the other, the called routine returns a string, and the value is displayed in a messagebox. Sometimes this contains a value and sometimes it is blank. First question is does this look right? Does the value go out of scope in the called routine before it is returned to the calling routine? If I am doing this wrong, could someone please provide sample code to show how I should be doing this? Thanks in advance...(btw, I am using V2.8.11 wxWidgets build)

Code: Select all

void ClassA::SomeMethod( void )
{
   ClassB *clB = new ClassB();
   wxString stringA;

   stringA = clB -> GetStringValue();

   wxMessageBox(stringA.GetData());

} // end SomeMethod



wxString ClassB::GetStringValue( void )
{
   wxString valueToReturn;

   valueToReturn.sprintf(wxT("Test"));

   return (valueToReturn);

} // end GetStringValue
EDIT by Auria: please use code tags

Re: problems returning wxString

Posted: Thu Dec 08, 2011 1:37 am
by Auria
I see nothing obviously wrong. Have you checked inside GetStringValue that the string is ok before returning it?

Then is this sample producing the error, or only some more complex code of yours? Because I assume that if you are using sprintf then you have some formatting to do, maybe there is a problem with the formatting

Re: problems returning wxString

Posted: Thu Dec 08, 2011 2:26 am
by TexasJimbo
I have checked from the called routine and there was a value in the wxString object before it returned to the calling routine. This sample code is not causing me a problem, I am just trying to simplify what my question is to ensure I am not doing anything obviously (to someone else) wrong. My actual code is more complex than this, but this is representative of what I am trying to do in that code.

Re: problems returning wxString

Posted: Thu Dec 08, 2011 8:25 am
by DerKleineNik
Are you using these methods in a thread? Sometimes thies causes problems as wxString is not threadsafe

Re: problems returning wxString

Posted: Thu Dec 08, 2011 2:25 pm
by TexasJimbo
No, I am not using threads.

Re: problems returning wxString

Posted: Thu Dec 08, 2011 2:38 pm
by doublemax
This sample code is not causing me a problem.
In that case it's not very helpful.

Maybe you're making a mistake like the one in this thread:
http://forums.wxwidgets.org/viewtopic.p ... 74#p137374

Re: problems returning wxString

Posted: Fri Dec 09, 2011 12:27 am
by TexasJimbo
I found a way around my problem, but not sure why my original code didn't work...
I was trying to build a new string using SetChar originally, but switched to Append.
Here is the routine and the offending statement is now commented out.

Thank you for the responses I received.

Code: Select all

wxString EncryptDecryptString::GetUnEncryptedSubString( void )
{
   int ctr;

   int uessIdx;
   int indexValue;
   wxChar tmpChar;

   unEncryptedSubString.Clear();
   unEncryptedSubString.Alloc(subStringLength);

   uessIdx = 0;
   for (ctr = subStringStartingPosition; ctr < subStringStartingPosition + subStringLength; ctr++) {
      tmpChar = fullEncryptedString.GetChar(ctr);
      indexValue = dataMap.Find(tmpChar);
//      unEncryptedSubString.SetChar(uessIdx, realMap.GetChar(indexValue));
      unEncryptedSubString.Append(realMap.GetChar(indexValue));
      ++uessIdx;
   } // end for

wxMessageBox(unEncryptedSubString.GetData());

   return (unEncryptedSubString);

} // end GetUnEncryptedSubString


Re: problems returning wxString

Posted: Fri Dec 09, 2011 12:42 am
by doublemax
SetChar() only works if there is already a character at that position, you can't use it when you start with an empty string.