Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Returning an array from a popup form

Posted on 2003-12-01
4
247 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Problem with SqlConnection 4 178
C++ mouse_event mouse look 7 95
DCT of  2D array using fftw in c++ 9 62
Gaming Software 1 19
Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
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.

839 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