Solved

Returning an array from a popup form

Posted on 2003-12-01
4
238 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
ID: 9849561
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
ID: 9849569
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
ID: 9849641
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
ID: 9849677
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

776 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