Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Returning an array from a popup form

Posted on 2003-12-01
4
Medium Priority
?
263 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 300 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

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…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
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 learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

715 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