Solved

Returning an array from a popup form

Posted on 2003-12-01
4
222 Views
Last Modified: 2010-04-01
How do I return a value from a popup dialog to the main form when the user closes the dialog? I know how to use DialogResult to return Yes, No, Cancel, etc, but is there a way to return any value? I need it to return a string array.

Thanks.
0
Comment
Question by:Statiscuro
  • 2
  • 2
4 Comments
 
LVL 8

Expert Comment

by:mnashadka
Comment Utility
Probably the easiest thing to do is to have the array as a data member in the dialog, populate the array duing OnOK() or whereever, and then have a get member function or make it public (which is common but not really recommended) -

// In your class declaration
class MyDialog : public MyBaseDialog
{
  ...
public:
  std::string *getarray() { return myarray; }

private:
  std::string myarray[20]; // Declare myarray as string/CString/char */whatever;
};

// In caller
MyDialog dlg;
if(dlg.DoModal() == IDOK)
{
  string *myarray = dlg.getarray();
}

Of course I would probably use a vector rather than an array, but this is basically how it would be done in that case as well.
0
 
LVL 39

Accepted Solution

by:
itsmeandnobodyelse earned 75 total points
Comment Utility
if you have something like this:

       CMyPopup myPopup;
       if (myPopup.DoModal() == IDOK)
       {
             // here the popup window is closed but the 'myPopup' object still lives
             vector<string> strings;
             // get a copy of the array of the popup
             if (myPopup->getStringArray(strings))
             {
                   // do something
             }            
       }

the member function getStringArray would be defined

     class CMyPopup
     {
         ....
     private:
          vector<string> m_strings;
          ....
     public:
          bool getStringArray(vector<string>& strings)
          {
                for (int i = 0; i < m_strings.size(); i++)
                     strings[i] = m_strings[i];
                return m_strings.size() > 0;
          }
          ....
     }

Regards, Alex
0
 
LVL 8

Expert Comment

by:mnashadka
Comment Utility
Alex's getStringArray could have some problems if strings isn't big enough to handle the number of entries in m_strings.  You could use the vector::resize member function, but an easier way would be just to use the copy function:
bool getStringArray(vector<string>& strings)
{
  copy(m_strings.begin(), m_strings.end(), back_inserter(strings));
  return ! m_strings.empty(); // A tiny bit more efficient
}
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
Comment Utility
Sorry, mnashadka is right.

I forgot that left side vector::operator[] will not automatically resize the array as my private array class does.

So you should either take that sequence from mnashadka or add

          bool getStringArray(vector<string>& strings)
          {
               strings.resize(m_strings.size());   // resizes if not already big enough
               for (....
               ..
          }

Regards, Alex


0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

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…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

771 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now