General Class

New Dialog is added from classwizard named CListSelection derived from
CDialog.Suppose I have added new functions as public member functions

void CListSelection::X()
void CListSelection::Y()

Now if I will create new object of type CListSelection in other file
I think I should be able to call these functions but it gives run time
errors.

Purpose of it I want to use this CListSelection class as a general,
global class.  

 CListSelection NewObject;
 NewObject.X();
 NewObject.DoModal();
devayaniAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

chensuCommented:
What run time errors does it give? There must be a bug in your program. Debug it.
0
VCGuruCommented:
Where does it give you runtime error. can u post some code?
0
bhatCommented:
Hi,
Give complete info. Whatever U have given is not enough.

0
Introduction to R

R is considered the predominant language for data scientist and statisticians. Learn how to use R for your own data science projects.

devayaniAuthor Commented:
void CTopTestView::OnInitialUpdate()
{
      CView::OnInitialUpdate();
// TODO: Add your specialized code here and/or call the base class
      
      CListSelection CompanySelection;

      /*It gives error here i.e Assertion failed*/

      CompanySelection.AddString(str);

      CompanySelection.DoModal();

}

void CListSelection::AddString(CString item)
{
      MessageBox(item);
}

CListSelection is class derived from CDialog.
It has one ListBox and three command buttons.
I want to make CListSelection general class which has its own functions
similar to CListBox.
0
captainkirkCommented:
Alot of times, you will get an assertion failure if the hWnd of the window you are creating is NULL, meaning that you have not called Create() yet before trying to access the window... you might want to rethink calling any access methods before DoModal() or redesign the dialog to be modeless - construct/create, then call access methods...
0
chensuCommented:
How come the error message is so difficult to come out? What exactly is the assertion failure? What version Visual C++ are you using?
0
devayaniAuthor Commented:
VC++ 5.0
0
abdijCommented:
I tried doing the same thing and it works fine for me.

Here is the code (whatever relevant)

// the ListSelection .h file

class CListSelection : public CDialog
{
// Construction
public:
      CListSelection(CWnd* pParent = NULL);   // standard constructor

      void CListSelection::X();
      void CListSelection::Y();


// Dialog Data
      //{{AFX_DATA(CListSelection)
      enum { IDD = IDD_DIALOGDilog };
            // NOTE: the ClassWizard will add data members here
      //}}AFX_DATA


// Overrides
      // ClassWizard generated virtual function overrides
      //{{AFX_VIRTUAL(CListSelection)
      protected:
      virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
      //}}AFX_VIRTUAL

// Implementation
protected:

      // Generated message map functions
      //{{AFX_MSG(CListSelection)
            // NOTE: the ClassWizard will add member functions here
      //}}AFX_MSG
      DECLARE_MESSAGE_MAP()
};


// the ListSelection.cpp

...
other functions
...

void CListSelection::X()
{
      AfxMessageBox("In X");
}

void CListSelection::Y()
{
      AfxMessageBox("In Y");
}

// the DlgExtView.cpp(DlgExt is my project name and i am handling the Left button down event)

void CDlgExtView::OnLButtonDown(UINT nFlags, CPoint point)
{

      CListSelection      listSelection;


      listSelection.X();
      listSelection.Y();

      listSelection.DoModal();

      CView::OnLButtonDown(nFlags, point);
}

I got no run time error . The 2 message boxes and then the dialog is displayed.

Even when i shifted the  "      CListSelection      listSelection;
" to make it global i did not get errors.

So there must be something else thats wrong. Definately its not the public member functions and their usage globally thats causing the problem.
Feel free to email the code if you want it debugged.
mail id is abdij_b@hotmail.com
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
VCGuruCommented:
Just an idea...
I guess you are getting error when you call CListSelection.DoModal();
Check the .h file of your CListSelection class, What is the value of IDD, do you have a valid resource there?.

Just add a dialog box into your workspace, Let us say IDD_DIALOG1, in your .h file modify the value of IDD to be equal to IDD_DIALOG1. Run the application...

Hope it helps
0
VCGuruCommented:
Just an idea...
I guess you are getting error when you call CListSelection.DoModal();
Check the .h file of your CListSelection class, What is the value of IDD, do you have a valid resource there?.

Just add a dialog box into your workspace, Let us say IDD_DIALOG1, in your .h file modify the value of IDD to be equal to IDD_DIALOG1. Run the application...

Hope it helps
0
devayaniAuthor Commented:
If I will use Create function to create modeless dialog and after that
ShowWindow it just displays momentarily and immediately disappears.
0
devayaniAuthor Commented:
I am sorry but first time I have not mentioned exactly what I want to do.In that CListSelection dialog box there is one listbox control,I want to all normal functions e.g AddString,SetCurSel etc. to it and
make that CListSelection dialog a dialog box so that I can use it anywhere.



So in actual AddStirng function I want to add string to that list box,in general case in InitDialog() I used to get pointer to that
ClistBox and using it I added string to it.But now as DoModal() is called afterwards I am not getting where to get pointer to that CListBox control.

void CListSelection::AddString(/*CString item,int id*/)
{
//      pCompanyList = (CListBox*)GetDlgItem(IDC_COMPANYLIST);
      pCompanyList->AddString("X");
//      MessageBox("H");
}

void CTopTestView::OnInitialUpdate()
{
      CListSelection CompanySelection;
      CompanySelection.AddString();
      CompanySelection.DoModal();
}
0
VCGuruCommented:
Why don't you add the string to the listbox in the OnInitDialog of your CListSelection...
0
abdijCommented:
Thanx for the info. I tried to simulate what you are trying to do. But thats not how you can make it work. Because when you try using the CListBox::AddString() it first checks for the Window handle which in your case is NULL. It simply asserts.
_AFXWIN_INLINE int CListBox::AddString(LPCTSTR lpszItem)
      {
// here is where it will fail There is simply no window
ASSERT(::IsWindow(m_hWnd));
 
return (int)::SendMessage(m_hWnd, LB_ADDSTRING, 0, (LPARAM)lpszItem); }

Her is what you can do. It is really a round about but it will work.

Have your own list (CList template can be used ) or an array (if you can limit the list entries to a limit (eg. 1000))

In the AddString() simply add the input parameter string to your list. In the OnInitDialog of the Dialog add the most recent entries to the list (you can maintain a counter to know this which is increamented in AddString or you can delete all the strings in the previous list and lod all of them anew (the latter is not presferable but the former is more harder to code).

The DeleteString fucntion can also be similary implemented.

But sorry other ClistBox function implementation require better logic. Not that it cannot be done. If your requirement is imperative to stick to the present design then here is how some of the functions can be implemented.

SetCurSel(): maintain a member variable (say int m_nSelectedID) to hold the Selection id. In the OnInitDialog() Set the selection to m_nSelectedID).

GetCurSel(): maintain a member variable (say int m_nCurSelectedID). Whenever the dialog is closed (map the DestroyWindow Message from ClassWizard to a function) get the current selection and store it in m_nCurSelectedID).

You have to have similar logic for other member functions if you want to implement it the way you seem to want.

Better change the design (vcguru is correct).

Hope this is OK for you. Feel free to ask.
Thanx
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
System Programming

From novice to tech pro — start learning today.