Code in constructor fails to execute. 
Author Message
 Code in constructor fails to execute.

SetJobDlg::SetJobDlg(CWnd* pParent /*=NULL*/) :
CDialog(SetJobDlg::IDD, pParent)
{
        //{{AFX_DATA_INIT(SetJobDlg)
        //}}AFX_DATA_INIT
        try
        {
                TCHAR pbuf[100];
                CFile file(_T("C:\\Program
Files\\Acad2000\\ExtendTabsData.dat"),
                CFile::modeReadWrite);
                file.Read(pbuf,100);
                SetDlgItemText(IDC_EDIT1,pbuf);
                file.Close();
        }
        catch(CFileException *e)
        {
                e->ReportError();
                e->Delete();
        }
        m_plansize.AddString("24X36");
        m_plansize.AddString("30X42");

Quote:
}

The following code refuses to execute. I also tried puuting the code
inside OnInitDialog which I am overriding. Can someone please, please
let me know why this is happening?


Sat, 05 Jun 2004 10:59:06 GMT  
 Code in constructor fails to execute.
Well you definitely can't do anything related to your controls in the
constructor.  There aren't any windows associated with the controls during
construction.  Both SetDlgItemText and AddString will fail.  OnInitDialog is
the proper place to initialize controls.

Perhaps you could post your OnInitDialog code and let us know what happens;
i.e. what doesn't work.

TFM3

Note: Spam-resistant e-mail address


Quote:
> SetJobDlg::SetJobDlg(CWnd* pParent /*=NULL*/) :
> CDialog(SetJobDlg::IDD, pParent)
> {
> file://{{AFX_DATA_INIT(SetJobDlg)
> file://}}AFX_DATA_INIT
> try
> {
> TCHAR pbuf[100];
> CFile file(_T("C:\\Program
> Files\\Acad2000\\ExtendTabsData.dat"),
> CFile::modeReadWrite);
> file.Read(pbuf,100);
> SetDlgItemText(IDC_EDIT1,pbuf);
> file.Close();
> }
> catch(CFileException *e)
> {
> e->ReportError();
> e->Delete();
> }
> m_plansize.AddString("24X36");
> m_plansize.AddString("30X42");
> }

> The following code refuses to execute. I also tried puuting the code
> inside OnInitDialog which I am overriding. Can someone please, please
> let me know why this is happening?



Sat, 05 Jun 2004 11:10:19 GMT  
 Code in constructor fails to execute.
Not terribly surpising. I see a SetDlgItemText being called, and this must necessarily be
on a control that does not exist, because the dialog can't be created until after the
constructor completes, and the control doesn't exist until the dialog is created. The code
you wrote looks like it belongs in the OnInitDialog handler of your dialog. Likewise, the
AddString operations cannot possibly work in a constructor.

Note that if you put this in OnInitDialog, and the error occurs, you should be prepared to
deal with several things:
        Lose the ReportError, it gives error messages uninitelligible to the user; do
                your own error reporting
        Even if the error occurs, the dialog will still come up; you will need to
                call CDialog::OnCancel to kill the dialog if the file fails to open, or
                you must be prepared to disable all the controls.

Also, a hardwired name like you show is almost always a totally losing idea; it assumes
that the end user would never install in D:\Program Files (most of my installs are in D:\,
I:\ or J:\; I install as little as possible on C:\, which is only 10GB, whereas D:\ is
30GB, I:\ is 40GB and J:\ is 40GB). Never make any assumptions about install directories.
You can default a string, but make sure it can always be specified by the end user. If
this is trying to read something about some existing product, go for the Registry entry
for that product to get the initial part of the path.
                                joe

Quote:

>SetJobDlg::SetJobDlg(CWnd* pParent /*=NULL*/) :
>CDialog(SetJobDlg::IDD, pParent)
>{
>    //{{AFX_DATA_INIT(SetJobDlg)
>    //}}AFX_DATA_INIT
>    try
>    {
>            TCHAR pbuf[100];
>            CFile file(_T("C:\\Program
>Files\\Acad2000\\ExtendTabsData.dat"),
>            CFile::modeReadWrite);
>            file.Read(pbuf,100);
>            SetDlgItemText(IDC_EDIT1,pbuf);
>            file.Close();
>    }
>    catch(CFileException *e)
>    {
>            e->ReportError();
>            e->Delete();
>    }
>    m_plansize.AddString("24X36");
>    m_plansize.AddString("30X42");
>}

>The following code refuses to execute. I also tried puuting the code
>inside OnInitDialog which I am overriding. Can someone please, please
>let me know why this is happening?

Joseph M. Newcomer [MVP]

Web: http://www3.pgh.net/~newcomer
MVP Tips: http://www3.pgh.net/~newcomer/mvp_tips.htm


Sat, 05 Jun 2004 14:13:22 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Executing C# code (VB Execute & Eval)

2. Q: Which constructor executes first???

3. Fire Events from ctrl's constructor failed

4. Debug assertion failed during in CPropretyPage constructor

5. Constructor fail

6. Debug assertion failed during in CPropretyPage constructor

7. Server.Execute fails in iHttpHandler?

8. Catch block fails to execute in release build

9. Task fails only when executed via CreateProcess?

10. Q: Task fails only when executed via CreateProcess?

11. Constructor code in memory??

12. Illegal code accepted (explicit constructor name)

 

 
Powered by phpBB® Forum Software