How to use the same instance of another class variable in two different button click events?

Hello

I know I'm doing this wrong, however I'm not sure how to use the same instance of Person both in button_RecordData_Click and in button_List_Click

This was easy on Java however I can't figure out how to do it in C++

I just want to (when RecordData button is clicked) take the value entered on textctrl_PatFirstNam, assign it to pFirstName variable (from the Person class), and then be able to show the value of pFirstName variable on a wxMessageBox when button List is clicked

Currently, the problem is that the wxMessageBox is blank



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Person
{
public:

    int pIdType;
    ::wxString pFirstName;
    ::wxString pLastName;

};

Person person = Person();


void Frame::On_button_RecordData_Click(wxCommandEvent& event)
{
    person.pFirstName = textctrl_PatFirstNam->GetValue().ToStdString();
}

void Frame::On_button_List_Click(wxCommandEvent& event)
{
    ::wxMessageBox(_(person.pFirstName));
}


Thanks!
Last edited on
Are you using the wxWidgets library? I’ve never approached to it and it seems I’m not the only one. I’d say you’d better ask your question in a wxWidgets forum.

The snippet syntax looks a bit weird.

::wxString
Is it recommended by wxWidgets to explicitly access the global namespace? What does it happens if you don’t do it?

Person person = Person();
What should do this line of code? Can’t you simply create an instance of Person?

…take the value entered on textctrl_PatFirstNam, assign it to pFirstName variable…
1
2
3
4
void MedCen__Frame::On_button_RecordData_Click(wxCommandEvent& event)
{
    person.pFirstName = textctrl_PatFirstNam->GetValue().ToStdString();
}

Apart from the BEM style double undescore in the class name (it’s the first time I see it in a C++ code), perhaps “textctrl_PatFirstNam” is not well defined or well assigned.
What value shows your debugger if you breakpoint the execution at that line?
What about using wxMessageBox() immediately after that assignment?

And again, from here:
https://docs.wxwidgets.org/3.0/group__group__funcmacro__dialog.html#ga193c64ed4802e379799cdb42de252647
it looks the wxMessageBox syntax shoud look more like
wxMessageBox(person.pFirstName);
than
::wxMessageBox(_(person.pFirstName));

What is that underscore for? Internationalization?
> Currently, the problem is that the wxMessageBox is blank
My understanding is the _ is a macro to do compile-time translation of fixed strings.

Besides, you really don't want to be translating user input.
If the user enters "Jack" in English, then it should still display as "Jack" in French.
Currently, the problem is that the wxMessageBox is blank
When person.pFirstName has a content wxMessageBox(...) shouldn't be blank. Hence it is probably the underscore.

Instead of making person a global variable you may consider make it a member variable of MedCen__Frame.
Thanks for your replies. I made many changes and don't remember which one was it, but I solved it :)
Topic archived. No new replies allowed.