Solved

Returning an array from a popup form

Posted on 2003-12-01
4
257 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
[X]
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
  • 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
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 viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

636 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