If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
-
TexasJimbo
- Knows some wx things

- Posts: 28
- Joined: Sun May 04, 2008 6:41 pm
Post
by TexasJimbo » Thu Dec 08, 2011 12:57 am
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
-
Auria
- Site Admin

- Posts: 6695
- Joined: Thu Sep 28, 2006 12:23 am
-
Contact:
Post
by Auria » Thu Dec 08, 2011 1:37 am
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
"Keyboard not detected. Press F1 to continue"
-- Windows
-
TexasJimbo
- Knows some wx things

- Posts: 28
- Joined: Sun May 04, 2008 6:41 pm
Post
by TexasJimbo » Thu Dec 08, 2011 2:26 am
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.
-
DerKleineNik
- Knows some wx things

- Posts: 29
- Joined: Fri Sep 09, 2011 9:59 am
Post
by DerKleineNik » Thu Dec 08, 2011 8:25 am
Are you using these methods in a thread? Sometimes thies causes problems as wxString is not threadsafe
-
TexasJimbo
- Knows some wx things

- Posts: 28
- Joined: Sun May 04, 2008 6:41 pm
Post
by TexasJimbo » Thu Dec 08, 2011 2:25 pm
No, I am not using threads.
-
TexasJimbo
- Knows some wx things

- Posts: 28
- Joined: Sun May 04, 2008 6:41 pm
Post
by TexasJimbo » Fri Dec 09, 2011 12:27 am
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
-
doublemax
- Moderator

- Posts: 15506
- Joined: Fri Apr 21, 2006 8:03 pm
- Location: $FCE2
Post
by doublemax » Fri Dec 09, 2011 12:42 am
SetChar() only works if there is already a character at that position, you can't use it when you start with an empty string.
Use the source, Luke!