Doc/View question

   I have reached a point where I need input on the following:
For some time I have used MFC created apps with Doc/View
and made my base view class as CFormView.  It was
convenient and quick, but I was putting all of my data variables in
the ViewClass since that was where the class wizard put all my
control variables and it seemed convenient in my ignorance. If I
had to access files I used ofstream from within my view class.
  So now it has become apparent that I have things backwards
(so it appears from what I read ) in that I should have been creating
data variables in the CDocument class.
    My problem in understanding some of the logistics is that I see
plenty of example code "getting" data into the view from the document,
but I don't see much on getting the user input from the view to the
document. At first I thought I could just make the view class a friend
of the document, but I think maybe that is not the way it is done. So
I am assuming you get a CDocument ptr as I have at the bottom of
shown code below,  (appreciate all comments on my understanding
of theory (or not understanding ) and code criticism of how I am
attempting to feed input data to variables in Document class.

CMyView::CMyView()                     // start of predicated view code
 : CFormView(CMyView::IDD)
{        // Edit Control Variables
  //{{AFX_DATA_INIT(CMyView)
  m_Invert_In_1 = 0.0;
  m_Invert_Out_1 = 0.0;
  m_InletNum = 0;
  //}}AFX_DATA_INIT
}

CMyView::~CMyView()
{
}

void CMyView::DoDataExchange(CDataExchange* pDX)
{
  CFormView::DoDataExchange(pDX);
  //{{AFX_DATA_MAP(CMyView)
  DDX_Text(pDX, IDC_Invert_In_1, m_Invert_In_1);
  DDX_Text(pDX, IDC_Invert_Out_1, m_Invert_Out_1);
  DDX_Text(pDX, IDC_ItemLabel, m_InletNum);
  //}}AFX_DATA_MAP
}                                          // end of predicated view code

void CMyView::OnEnter()     // Begin question code area
{
  UpdateData(TRUE);
  // Code here is asking for comment and design theory input //
  // I am moving edit control variables input to my data variables
  CMyDoc* DocPtr = GetDocument();
  DocPtr->InletA->dInvert_In_1  =  m_Invert_In_1;
  DocPtr->InletA->dInvert_Out_1 =  m_Invert_Out_1;
  DocPtr->InletA->uiInletNum  =  m_InletNum;
}
--------------- predicated variables in document class
class CMyDoc : public CDocument
{
  protected: // create from serialization only
  CMyDoc();
  DECLARE_DYNCREATE(CMyDoc)

// Attributes
  public:

  struct Inlet
   { double dInvert_In_1;
     double dInvert_Out_1;
     unsigned int uiInletNum;
   };

  Inlet InletA;
.................rest of document class...... 


0
RB
5/5/2010 2:12:46 AM
vc.mfc 33608 articles. 0 followers. Follow

11 Replies
1757 Views

Similar Articles

[PageSpeed] 49

"RB" <NoMail@NoSpam> wrote in message 
news:uZPz7h$6KHA.2220@TK2MSFTNGP04.phx.gbl...
>  // Code here is asking for comment and design theory input //
>  // I am moving edit control variables input to my data variables
>  CMyDoc* DocPtr = GetDocument();
>  DocPtr->InletA->dInvert_In_1  =  m_Invert_In_1;
>  DocPtr->InletA->dInvert_Out_1 =  m_Invert_Out_1;
>  DocPtr->InletA->uiInletNum  =  m_InletNum;

Yup, this is pretty much how you do it.  Although you may prefer to create 
methods in your document which the view calls to get/set the data, instead 
of accessing the member data directly.  This encapsulates the document 
implementation so you can just change it in the document without affecting 
all the views (remember you can have multiple view classes showing different 
aspects of the same document) interaction.

-- David 

0
David
5/5/2010 2:26:00 AM
See below...
On Tue, 4 May 2010 22:12:46 -0400, "RB" <NoMail@NoSpam> wrote:

>   I have reached a point where I need input on the following:
>For some time I have used MFC created apps with Doc/View
>and made my base view class as CFormView.  It was
>convenient and quick, but I was putting all of my data variables in
>the ViewClass since that was where the class wizard put all my
>control variables and it seemed convenient in my ignorance. 
****
I consider using data variables to be a serious design error.  I avoid them entirely.  You
should think about whether or not their existence makes sense.  I consider the whole
UpdateData mechanism to be a seriously deep design failure.  I have yet to find a reason
that it could ever make sense within the context of a dialog or CFormView-derived class.
Their only value, which is marginal at best, is for a very limited subset of control types
(edit controls, static controls, and check boxes; they are completely useless for any
other kind of control)
****
>If I
>had to access files I used ofstream from within my view class.
****
Probably the wrong decision.  What I do is keep the values in variables in the
CDocument-derived class.  In the serialize routine I simply call UpdateAllViews() using a
specific lHint and pHint value which are not (0,NULL), and when this particular
combination is detected in my OnUpdate handler, I simply use operations like
GetWindowText, GetCurSel, and similar operations to copy the values out of the controls
and place them in variables in the CDocument-derived classs.  In this way, onlly the
document understands how to read and write the data, and there is never any use of streams
in the view.  Data transfer to and from the document are managed by the document class,
which is where it should be handled.

When the CFormView starts up, the OnInitialUpdate loads the controls from the variables in
the CDocument-derived class.  

I do not believe in the DDX and DDV mechanisms at all, except for the DDX_Control calls to
bind the controls to control variables.
****
>  So now it has become apparent that I have things backwards
>(so it appears from what I read ) in that I should have been creating
>data variables in the CDocument class.
>    My problem in understanding some of the logistics is that I see
>plenty of example code "getting" data into the view from the document,
>but I don't see much on getting the user input from the view to the
>document. At first I thought I could just make the view class a friend
>of the document, 
****
NO!!!! This would require the CDocument-derived class know about the views, and this is
COMPLETELY WRONG!. Use UpdateAllViews to handle the transfer, as described.  NEVER include
a view header file in the document class; if you think you have to, your design is wrong!
*****
>but I think maybe that is not the way it is done. So
>I am assuming you get a CDocument ptr as I have at the bottom of
>shown code below,  (appreciate all comments on my understanding
>of theory (or not understanding ) and code criticism of how I am
>attempting to feed input data to variables in Document class.
>
>CMyView::CMyView()                     // start of predicated view code
> : CFormView(CMyView::IDD)
>{        // Edit Control Variables
>  //{{AFX_DATA_INIT(CMyView)
>  m_Invert_In_1 = 0.0;
>  m_Invert_Out_1 = 0.0;
****
I presume these values have types associated with them...
****
>  m_InletNum = 0;
>  //}}AFX_DATA_INIT
>}
>
>CMyView::~CMyView()
>{
>}
>
>void CMyView::DoDataExchange(CDataExchange* pDX)
>{
>  CFormView::DoDataExchange(pDX);
>  //{{AFX_DATA_MAP(CMyView)
>  DDX_Text(pDX, IDC_Invert_In_1, m_Invert_In_1);
>  DDX_Text(pDX, IDC_Invert_Out_1, m_Invert_Out_1);
>  DDX_Text(pDX, IDC_ItemLabel, m_InletNum);
>  //}}AFX_DATA_MAP
>}                                          // end of predicated view code
>
>void CMyView::OnEnter()     // Begin question code area
>{
>  UpdateData(TRUE);
***
I consider writing UpdateData to be a fundamental design error.  It is based on a set of
myths that I simply do not believe; that it makes sense to EVER transfer *every* data
value either to the controls, or from the controls.  This has nasty implications when the
values interact.  Read my essay on Dialog Control Management on my MVP Tips site.
****
>  // Code here is asking for comment and design theory input //
>  // I am moving edit control variables input to my data variables
>  CMyDoc* DocPtr = GetDocument();
>  DocPtr->InletA->dInvert_In_1  =  m_Invert_In_1;
>  DocPtr->InletA->dInvert_Out_1 =  m_Invert_Out_1;
>  DocPtr->InletA->uiInletNum  =  m_InletNum;
***
THis is incosnsitent with your declaration; it should be
	DocPtr->InLetA.dInvert_In_1 = m_Invert_In_1;

because InLetA is not a pointer varaible.  It is not clear why you need to know the
internals of the data structure that holds these values.  
>}

****
I would do it using approximately the same idea, but do it in the OnUpdate handler:

void CMyView::OnUpdate(CView * view, LPARAM lHint, CObject * pHint)
{
   if(lHint == 0 && pHint == NULL)
     {
      CView::OnUpdate(view, lHint, pHint);
      return;
     }
    switch(lHint)
        { /* lHint */
         case VALUES_TO_DOCUMENT:
            {
             CMyDoc * DocPtr = ...etc.
             DocPtr->SetInvertIn(...);
             DocPtr->SetInvertOut(...);
             DocPtr->SetInletNum(...);
             // I prefer to use set/get methods rather than directly access variables
             // I might also do this as
             // DocPtr->SetInLetValues(..., ..., ....);
             // and not worry about the specification of the structure at all!
            }
            break;
         case ...
            break;
         default:
            ...maybe ASSERT(FALSE) here....your choice
            break;
        } /* lHint */
} // CMyView::OnUpdate
>--------------- predicated variables in document class
>class CMyDoc : public CDocument
>{
>  protected: // create from serialization only
>  CMyDoc();
>  DECLARE_DYNCREATE(CMyDoc)
>
>// Attributes
>  public:
>
>  struct Inlet
>   { double dInvert_In_1;
>     double dInvert_Out_1;
>     unsigned int uiInletNum;
>   };
>
>  Inlet InletA;
>................rest of document class...... 
>
Joseph M. Newcomer [MVP]
email: newcomer@flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
0
Joseph
5/5/2010 4:23:15 AM
On May 5, 4:12=A0am, "RB" <NoMail@NoSpam> wrote:
> =A0 =A0I have reached a point where I need input on the following:
> For some time I have used MFC created apps with Doc/View
> and made my base view class as CFormView. =A0It was
> convenient and quick, but I was putting all of my data variables in
> the ViewClass since that was where the class wizard put all my
> control variables and it seemed convenient in my ignorance. If I
> had to access files I used ofstream from within my view class.
> =A0 So now it has become apparent that I have things backwards
> (so it appears from what I read ) in that I should have been creating
> data variables in the CDocument class.
> =A0 =A0 My problem in understanding some of the logistics is that I see
> plenty of example code "getting" data into the view from the document,
> but I don't see much on getting the user input from the view to the
> document. At first I thought I could just make the view class a friend
> of the document, but I think maybe that is not the way it is done. So
> I am assuming you get a CDocument ptr as I have at the bottom of
> shown code below, =A0(appreciate all comments on my understanding
> of theory (or not understanding ) and code criticism of how I am
> attempting to feed input data to variables in Document class.
>
> CMyView::CMyView() =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 // start of pr=
edicated view code
> =A0: CFormView(CMyView::IDD)
> { =A0 =A0 =A0 =A0// Edit Control Variables
> =A0 //{{AFX_DATA_INIT(CMyView)
> =A0 m_Invert_In_1 =3D 0.0;
> =A0 m_Invert_Out_1 =3D 0.0;
> =A0 m_InletNum =3D 0;
> =A0 //}}AFX_DATA_INIT
>
> }
>
> CMyView::~CMyView()
> {
>
> }
>
> void CMyView::DoDataExchange(CDataExchange* pDX)
> {
> =A0 CFormView::DoDataExchange(pDX);
> =A0 //{{AFX_DATA_MAP(CMyView)
> =A0 DDX_Text(pDX, IDC_Invert_In_1, m_Invert_In_1);
> =A0 DDX_Text(pDX, IDC_Invert_Out_1, m_Invert_Out_1);
> =A0 DDX_Text(pDX, IDC_ItemLabel, m_InletNum);
> =A0 //}}AFX_DATA_MAP
>
> } =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0=
 =A0 =A0 =A0// end of predicated view code
>
> void CMyView::OnEnter() =A0 =A0 // Begin question code area
> {
> =A0 UpdateData(TRUE);
> =A0 // Code here is asking for comment and design theory input //
> =A0 // I am moving edit control variables input to my data variables
> =A0 CMyDoc* DocPtr =3D GetDocument();
> =A0 DocPtr->InletA->dInvert_In_1 =A0=3D =A0m_Invert_In_1;
> =A0 DocPtr->InletA->dInvert_Out_1 =3D =A0m_Invert_Out_1;
> =A0 DocPtr->InletA->uiInletNum =A0=3D =A0m_InletNum;}
>
> --------------- predicated variables in document class
> class CMyDoc : public CDocument
> {
> =A0 protected: // create from serialization only
> =A0 CMyDoc();
> =A0 DECLARE_DYNCREATE(CMyDoc)
>
> // Attributes
> =A0 public:
>
> =A0 struct Inlet
> =A0 =A0{ double dInvert_In_1;
> =A0 =A0 =A0double dInvert_Out_1;
> =A0 =A0 =A0unsigned int uiInletNum;
> =A0 =A0};
>
> =A0 Inlet InletA;
> ................rest of document class......

That should cover it. Don't forget "if (UpdateData(TRUE)) { transfer
from view to doc } ("if" is important, that's how UpdateData is
supposed to work). Also, don't forget to call UpdateAllViews upon the
change to the document (that's how doc/view is supposed to work).

Goran.
0
Goran
5/5/2010 6:38:39 AM
>you may prefer to create methods in your document which the view calls to get/set the data, instead of accessing the member data 
>directly.  This encapsulates the document implementation so you can just change it in the document without affecting all the views 
>(remember you can have multiple view classes showing different aspects of the same document) interaction.
> David

Thanks David, I have read what you are referring to and as I move along I
want to embrace that facet also. I have an experimental app going that I am
using to feel my way out of the reverse logic that I had been using which was
pretty much "View" everything and "Document" nothing. 


0
RB
5/5/2010 1:48:03 PM
>That should cover it. Don't forget "if (UpdateData(TRUE)) { transfer
>from view to doc } ("if" is important, that's how UpdateData is
>supposed to work). Also, don't forget to call UpdateAllViews upon the
>change to the document (that's how doc/view is supposed to work).
>Goran.

Thanks Goran, that is a good point, and a weakness of mine, especially
when getting experimental code for posting (no excuse, just honesty) 


0
RB
5/5/2010 1:50:47 PM
> I consider using data variables to be a serious design error.  I avoid them entirely. You
> should think about whether or not their existence makes sense.  I consider the whole
> UpdateData mechanism to be a seriously deep design failure.  I have yet to find a reason
> that it could ever make sense within the context of a dialog or CFormView-derived class.
> Their only value, which is marginal at best, is for a very limited subset of control types
> (edit controls, static controls, and check boxes; they are completely useless for any
> other kind of control)
> ****

Well, Joe first I must thank you for giving me what I asked for (as usual ) but the
context of the above pretty much flys right over my head. When you read this you
probably are either laughing or sighing.  But without burdening your super helpful
energetic input methods, could you briefly elaborate on specifically you mean by
not using data variables ?  Are you referrering to data variables for the controls ?
And I am correct in surmizing that you directing me further as to how to implement
this in the rest of your reply below ?

> ****
> Probably the wrong decision.  What I do is keep the values in variables in the
> CDocument-derived class.  In the serialize routine I simply call UpdateAllViews()
> using a > specific lHint and pHint value which are not (0,NULL), and when this particular
> combination is detected in my OnUpdate handler, I simply use operations like
> GetWindowText, GetCurSel, and similar operations to copy the values out of the controls
> and place them in variables in the CDocument-derived classs.  In this way, onlly the
> document understands how to read and write the data, and there is never any use of streams
> in the view.  Data transfer to and from the document are managed by the document class,
> which is where it should be handled.
> When the CFormView starts up, the OnInitialUpdate loads the controls from the variables in
> the CDocument-derived class.
>
> I do not believe in the DDX and DDV mechanisms at all, except for the DDX_Control calls to
> bind the controls to control variables.
> ****

Ok.... I will have to dwell on this awhile.

>> At first I thought I could just make the view class a friend of the document,
> ****
> NO!!!! This would require the CDocument-derived class know about the views, and this is
> COMPLETELY WRONG!. Use UpdateAllViews to handle the transfer, as described.
> NEVER include a view header file in the document class; if you think you have to, your
> design is wrong!
> *****

Ok, I had a feeling as such

> ***
> I consider writing UpdateData to be a fundamental design error.  It is based on a set of
> myths that I simply do not believe; that it makes sense to EVER transfer *every* data
> value either to the controls, or from the controls.  This has nasty implications when the
> values interact.  Read my essay on Dialog Control Management on my MVP Tips site.
> ****

Ok I will check Tips site out

>****
>>  DocPtr->InletA->dInvert_In_1  =  m_Invert_In_1;
>>  DocPtr->InletA->dInvert_Out_1 =  m_Invert_Out_1;
>>  DocPtr->InletA->uiInletNum  =  m_InletNum;
> ***
> THis is incosnsitent with your declaration; it should be
> DocPtr->InLetA.dInvert_In_1 = m_Invert_In_1;
>
> because InLetA is not a pointer
> ****

Yes that is correct, your eagle eye caught it.  While we are here
would you be so kind as to tell me how I might write this if I changed
InletA to an array of structs as in
Inlet InletA[10];
 In other words how would I first even declare properly the StructArrayPtr?

Inlet* InletA[10];   I sense this would only be an array of pts and not
                             what I want.
And then how would I properly write the update code ?
DocPtr->InletA[2] ?? dInvert_In_1 = m_Invert_In_1;
--------------
And finally to this last input (below) from you. I am saving this and will study
over it. But I have the following questions:
1. If I implement the below then I surmise I should call the OnUpDate from
    my OnEnter handler ? (from a control button click)
2. Notwithstanding the previous input you have given me, is there an
    additional key directional thought focus item that you could give me as
    why this method is better than, or safer than, or logistically better than
    using DDX

**********
> I would do it using approximately the same idea, but do it in the OnUpdate handler:
>
> void CMyView::OnUpdate(CView * view, LPARAM lHint, CObject * pHint)
> {
>   if(lHint == 0 && pHint == NULL)
>     {
>      CView::OnUpdate(view, lHint, pHint);
>      return;
>     }
>    switch(lHint)
>        { /* lHint */
>         case VALUES_TO_DOCUMENT:
>            {
>             CMyDoc * DocPtr = ...etc.
>             DocPtr->SetInvertIn(...);
>             DocPtr->SetInvertOut(...);
>             DocPtr->SetInletNum(...);
>             // I prefer to use set/get methods rather than directly access variables
>             // I might also do this as
>             // DocPtr->SetInLetValues(..., ..., ....);
>             // and not worry about the specification of the structure at all!
>            }
>            break;
>         case ...
>            break;
>         default:
>            ...maybe ASSERT(FALSE) here....your choice
>            break;
>        } /* lHint */
> } // CMyView::OnUpdate
************ 


0
RB
5/5/2010 2:55:47 PM
See below,,,
On Wed, 5 May 2010 10:55:47 -0400, "RB" <NoMail@NoSpam> wrote:

>> I consider using data variables to be a serious design error.  I avoid them entirely. You
>> should think about whether or not their existence makes sense.  I consider the whole
>> UpdateData mechanism to be a seriously deep design failure.  I have yet to find a reason
>> that it could ever make sense within the context of a dialog or CFormView-derived class.
>> Their only value, which is marginal at best, is for a very limited subset of control types
>> (edit controls, static controls, and check boxes; they are completely useless for any
>> other kind of control)
>> ****
>
>Well, Joe first I must thank you for giving me what I asked for (as usual ) but the
>context of the above pretty much flys right over my head. When you read this you
>probably are either laughing or sighing.  But without burdening your super helpful
>energetic input methods, could you briefly elaborate on specifically you mean by
>not using data variables ?  Are you referrering to data variables for the controls ?
***
Control variables would NEVer be public; it is a complete failure of the IDE design (one
of many too numerous to elaborate on here) to default to "public" for a control varfiable
or for an event handler.  It goes beyond completely stupid into the realm of deeply
irresponsible  I attrribute it to a lack of adult supervision of designs,

the basic contrast here is

class CWhatever  : public CView {
     public:
	int SomeValue;
}

CWhatever * p;

p->SomeValue = 3;

and the setter/getter model:

class CWhatever : public CView {
	protected:
	int SomeValue;
	public:
	void SetSomeValue(int newvalue);
	int GetSomeValue();
};

 By having the functions, you can do many things, e.g.,

void CWhatever::SetSomeValue(int newvalue)
   [
    if(SomeValue == newvalue)
      return;
   SomeValue = newvalue;
   if(GetSafeHwnd() != NULL)
      Invalidate()l
  }

CWhatever * p;
	p->SetSomeValue(3);

 A common childish belief is that the setter/gerr code is "less efficient" than straight
assignmen to a member variablet, which means that there is a belief that a couple
nanoseconds to do the call and return actually matter; also, modern compiler using Link
Time Code Generation (LTCG) will actually generate the code inline anyway, so the
rationale never made sense in the past and certainly doesn't make sense today.  Such myths
are created by PDP-11 programmers who still teach programming but who never grew up.

Note that the setter does not need to know if the implementation needs to do anything in
response to the change; the setter just calls SetSomeValue and Magic Happens.
				joe
*****
>And I am correct in surmizing that you directing me further as to how to implement
>this in the rest of your reply below ?
>
>> ****
>> Probably the wrong decision.  What I do is keep the values in variables in the
>> CDocument-derived class.  In the serialize routine I simply call UpdateAllViews()
>> using a > specific lHint and pHint value which are not (0,NULL), and when this particular
>> combination is detected in my OnUpdate handler, I simply use operations like
>> GetWindowText, GetCurSel, and similar operations to copy the values out of the controls
>> and place them in variables in the CDocument-derived classs.  In this way, onlly the
>> document understands how to read and write the data, and there is never any use of streams
>> in the view.  Data transfer to and from the document are managed by the document class,
>> which is where it should be handled.
>> When the CFormView starts up, the OnInitialUpdate loads the controls from the variables in
>> the CDocument-derived class.
>>
>> I do not believe in the DDX and DDV mechanisms at all, except for the DDX_Control calls to
>> bind the controls to control variables.
>> ****
>
>Ok.... I will have to dwell on this awhile.
****
See my essays on the use of control variables, dialog control management, and the use of
constraint-management patterns on my MVP tips site.  
*****
>
>>> At first I thought I could just make the view class a friend of the document,
>> ****
>> NO!!!! This would require the CDocument-derived class know about the views, and this is
>> COMPLETELY WRONG!. Use UpdateAllViews to handle the transfer, as described.
>> NEVER include a view header file in the document class; if you think you have to, your
>> design is wrong!
>> *****
>
>Ok, I had a feeling as such
>
>> ***
>> I consider writing UpdateData to be a fundamental design error.  It is based on a set of
>> myths that I simply do not believe; that it makes sense to EVER transfer *every* data
>> value either to the controls, or from the controls.  This has nasty implications when the
>> values interact.  Read my essay on Dialog Control Management on my MVP Tips site.
>> ****
>
>Ok I will check Tips site out
>
>>****
>>>  DocPtr->InletA->dInvert_In_1  =  m_Invert_In_1;
>>>  DocPtr->InletA->dInvert_Out_1 =  m_Invert_Out_1;
>>>  DocPtr->InletA->uiInletNum  =  m_InletNum;
>> ***
>> THis is incosnsitent with your declaration; it should be
>> DocPtr->InLetA.dInvert_In_1 = m_Invert_In_1;
>>
>> because InLetA is not a pointer
>> ****
>
>Yes that is correct, your eagle eye caught it.  While we are here
>would you be so kind as to tell me how I might write this if I changed
>InletA to an array of structs as in
>Inlet InletA[10];
> In other words how would I first even declare properly the StructArrayPtr?
****
In this case, I'd definitely use setters and getters!
*****
>
>Inlet* InletA[10];   I sense this would only be an array of pts and not
>                             what I want.
****
I have a strong belief that if you ever write a declaration in C++ of the form
	type name[complie-time-constant]
you are not using C++ correctly.,  Instead, consider CArray or std::vector
implementations!  Preferrably the latter.
*****
>And then how would I properly write the update code ?
>DocPtr->InletA[2] ?? dInvert_In_1 = m_Invert_In_1;
****
DocPtr->inLetA[2]->dInver_In_1 =

because InLetA[2] is a pointer type!  But you shouldn't be writing code like this!  Not in
C++. Using setters and getters, you might do

int n = DocPtr->Add(...something...?);
DocPtr->SetInvertIn(n, ...expression...);

Much cleaner, and avoids all kinds of nasty problems.  For example,your setter can
implement array bounds checks and be a BOOL; return FALSE if there is a bounds error, and
you can write
	VERIFY(DocPtr->SetInVertIn(,,.));
and it will catch bugs during debugging, and at least not do overruns on release,
****
>--------------
>And finally to this last input (below) from you. I am saving this and will study
>over it. But I have the following questions:
>1. If I implement the below then I surmise I should call the OnUpDate from
>    my OnEnter handler ? (from a control button click)
****
OnUpdate is called by the framework (usually, with NULL,0,NULL) during the view setup, so
you don't need to worry about it.  If called with other than (0, NULL) as the last two
arguments, you use this to determine if you must capture the values to the document, or
the document has new values to be placed in the controls, etc.
****
>2. Notwithstanding the previous input you have given me, is there an
>    additional key directional thought focus item that you could give me as
>    why this method is better than, or safer than, or logistically better than
>    using DDX
****
DDX does not allo for you to detect or react to interacting changes; e.g., setting the
check box enables the dropdown list.  If you start distributing this code throughout your
dialog/formview, eventually the code diverges, and you end up with six different
algorithms, none of which are quite right. My constraint-management pattern means that
there is exactly ONE place where interactions are managed and predicates are evaluated.

And DDX doesn't work right for dropdown lists, list controls, tree controls, etc. so using
it only in the very limited cases it works in fools you into thinking it makes sense.
				joe
****
>
>**********
>> I would do it using approximately the same idea, but do it in the OnUpdate handler:
>>
>> void CMyView::OnUpdate(CView * view, LPARAM lHint, CObject * pHint)
>> {
>>   if(lHint == 0 && pHint == NULL)
>>     {
>>      CView::OnUpdate(view, lHint, pHint);
>>      return;
>>     }
>>    switch(lHint)
>>        { /* lHint */
>>         case VALUES_TO_DOCUMENT:
>>            {
>>             CMyDoc * DocPtr = ...etc.
>>             DocPtr->SetInvertIn(...);
>>             DocPtr->SetInvertOut(...);
>>             DocPtr->SetInletNum(...);
>>             // I prefer to use set/get methods rather than directly access variables
>>             // I might also do this as
>>             // DocPtr->SetInLetValues(..., ..., ....);
>>             // and not worry about the specification of the structure at all!
>>            }
>>            break;
>>         case ...
>>            break;
>>         default:
>>            ...maybe ASSERT(FALSE) here....your choice
>>            break;
>>        } /* lHint */
>> } // CMyView::OnUpdate
>************ 
>
Joseph M. Newcomer [MVP]
email: newcomer@flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
0
Joseph
5/5/2010 7:12:54 PM
Oh ok, if I get the jest of input you are saying to make all data vars
either protected or private and use getter setter funcs to access
and/or change them. Well that makes sense, I can't argue with that.
And I can see the advantages to such as opposed to just assignment
by any means, DDX or otherwise.

However I am still a bit out in left field on your latest instruction on
CArray or std::vector, and not just because I have never used either
one of them, but why is the structure array not ok if I access it using
setter getter paradigm.  I am trying to write a calculator of pipe
inverts and the struct type seems to fit what I need. But I have many
manholes and need a structure array to reference all of them.
And by the way just for my knowledge. I see what you told me about
assigning the structure array member using a structure array ptr,
DocPtr->InletA[2]->dInver_In_1 =
But how would a person declare a ptr to a structure array.
Struct InletA[10];
Inlet* InletA[10];  I don't think this is it ??
Inlet (*InletA)[10];  ??  How would one do this ?
( I am at work now and do not have access to my compiler so my
   suggestions may have given me errors to answer my questions,
   but none the less would like your input on this ) 


0
RB
5/6/2010 2:08:28 PM
See below...
On Thu, 6 May 2010 10:08:28 -0400, "RB" <NoMail@NoSpam> wrote:

>Oh ok, if I get the jest of input you are saying to make all data vars
>either protected or private and use getter setter funcs to access
>and/or change them. Well that makes sense, I can't argue with that.
>And I can see the advantages to such as opposed to just assignment
>by any means, DDX or otherwise.
>
>However I am still a bit out in left field on your latest instruction on
>CArray or std::vector, and not just because I have never used either
>one of them, but why is the structure array not ok if I access it using
>setter getter paradigm.  
****
Generally, because there is no bounds checking done in pure C.  Also, it puts large
objects on the stack (there was some example of this posted here a few weeks ago, where
somebody had a local array of 17x17xmassive structure on the stack and was getting stack
overflows).  Overall, the advantage of CArray or std::vector is that the stack space
consumed is very small (a few bytes) and the data is always on the heap.
*****
>I am trying to write a calculator of pipe
>inverts and the struct type seems to fit what I need. But I have many
>manholes and need a structure array to reference all of them.
****
If you don't know how many you have in advance, a compile-time-constant array is a Really
Bad Idea.  Either you need to make it big enough to hold the absolute maximum you might
ever encounter (consuming massive stack space) or be prepared to deal with the fact that
the program is not adequate to the task because the array bounds, which must be set at
compile time, are too small.  CArray::SetAt will let you set any array element, and do
bounds checking, and CArray::SetAtGrow will expand the array to the specified size.
CArray::Add will expand it until memory exhausts.  There are analogous features for
std::vector.

The old-fashioned array declared on the stack is just that: old-fashioned, replaced by far
more flexible, robust, and powerful alternatives.  The key here is to stop thinking like a
C programmer and start thinking like a C++ programmer.
****
>And by the way just for my knowledge. I see what you told me about
>assigning the structure array member using a structure array ptr,
>DocPtr->InletA[2]->dInver_In_1 =
>But how would a person declare a ptr to a structure array.
>Struct InletA[10];
>Inlet* InletA[10];  I don't think this is it ??
***
This declares an array of pointers to structures, but you actually have to make sure the
elements you care about are initialized with pointers that point to actual instances of
the structure, otherwise you will have serious errors in the code.

e.g.

Inlet* InletA[10];
InletA[2]->whatever = 0; // access fault if you are lucky!

The above fails because InletA[2] has no valid value (in debug mode, it is initialized to
0xCCCCCCCC if it is declared on the stack, 0xCDCDCDCD if it is on the heap)

You would have to do

Inlet * InletA[10];
InletA[2] = new InletA;
NOW you can write
InletA[2]->whatever = 0;

because there is a real object there.

But I would never write code like this.  I might do

CArray<Inlet> InletA;

InletA.SetSize(10);

and now I can write

InletA[2].whatever = 0;

or I might write

std::vector<Inlet> InletA;
InletA.resize(10);

The choice of using objects or pointers to objects is another dimension of design, but
first you have to start programming in C++, not primitve 1975-style C.
					joe
*****
>Inlet (*InletA)[10];  ??  How would one do this ?
>( I am at work now and do not have access to my compiler so my
>   suggestions may have given me errors to answer my questions,
>   but none the less would like your input on this ) 
>
Joseph M. Newcomer [MVP]
email: newcomer@flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
0
Joseph
5/6/2010 8:58:29 PM
Oh well ok this (your reply below) does sound very convicting, I am going to have
to encompass this also into my immediate learning curve. I did not realize the whole
Class was created on the stack (or maybe all of it isn't, probably the code isn't ) but
if there is a chance of substantial stack overload I don't want to spend time writing
something for that.
  So then it appears that I can put structs of different member types into CArray
and/or  std::vector and not just the usual data types.  I will have to begin reading
and trying some of this.
Thank you.

--------------------------------------------------------------------------------
"Joseph M. Newcomer"
> Generally, because there is no bounds checking done in pure C.  Also, it puts large
> objects on the stack (there was some example of this posted here a few weeks ago, where
> somebody had a local array of 17x17xmassive structure on the stack and was getting stack
> overflows).  Overall, the advantage of CArray or std::vector is that the stack space
> consumed is very small (a few bytes) and the data is always on the heap.

> If you don't know how many you have in advance, a compile-time-constant array is a Really
> Bad Idea.  Either you need to make it big enough to hold the absolute maximum you might
> ever encounter (consuming massive stack space) or be prepared to deal with the fact that
> the program is not adequate to the task because the array bounds, which must be set at
> compile time, are too small.  CArray::SetAt will let you set any array element, and do
> bounds checking, and CArray::SetAtGrow will expand the array to the specified size.
> CArray::Add will expand it until memory exhausts.  There are analogous features for
> std::vector.
>
> CArray<Inlet> InletA;
> InletA.SetSize(10);
> std::vector<Inlet> InletA;
> InletA.resize(10);


0
RB
5/6/2010 11:26:14 PM
If you do sizeof(classname) you will find out how many bytes an instance of the class
requires.  This is all the non-static data, plus, if there are virtual methods, a slot for
the vptr (virtual function table pointer).  Then multiply this times the number of
elements in the array to get how many bytes of stack space it requires.  A simple model is
that it is the sum of the sizeof() of all the nonstatic data components.  THe problem with
the 17x17 problem was that many of the data components were themselves large structs, so
there was a lot of bytes used by every instance.

It is the nature of the C subset of C++ that arrays are allocated on the stack as a
sequence of bytes that is long enough to hold the entire contents of the array.  This has
always been true since the first C compilers appeared in the 1970s.

The biggest problem is something that is called "C Programmer's Disease" (see: The New
Hacker's Dictionary) which is creating an array of n elements and writing values in it
where the index exceeds (n - 1) and most commonly the failed index is equal to n.  The two
alternatives if your array is too small are The C Programmer's Disease or having your
program come out and tell the user "Too much information!" (not some useless dialog box
that says "Bounds check errror in function .... file .... line ...., which is not
considered User Friendly).  Typically I do a throw of an exception which includes the
file, line and function name so I can log the problem and otherwise do not expose the user
to the details.

You can put *anything* into a CArray or std::vector; the parameter to the template is the
data type.  So you can do
	std::vector<int> Things;
	typedef struct {...complex stuff...} SomeType;
	std::vector<SomeType> Stuff;
	std::vector<SomeType *> MoreStuff;

there is nothing in CArray or std::vector to suggest that you CAN'T use an arbitrary type
in them!  So you can use "the usual data types" because EVERY type you define is a "usual
data type" for this purpose!

Note that for std::map, you need to have a value-comparison function defined on the type
(think it has to be a virtual method lessstr, but I have to look this up each time), but
that is easy to write.  But no such function is needed if they are in a std::vector.
				joe

On Thu, 6 May 2010 19:26:14 -0400, "RB" <NoMail@NoSpam> wrote:

>Oh well ok this (your reply below) does sound very convicting, I am going to have
>to encompass this also into my immediate learning curve. I did not realize the whole
>Class was created on the stack (or maybe all of it isn't, probably the code isn't ) but
>if there is a chance of substantial stack overload I don't want to spend time writing
>something for that.
>  So then it appears that I can put structs of different member types into CArray
>and/or  std::vector and not just the usual data types.  I will have to begin reading
>and trying some of this.
>Thank you.
>
>--------------------------------------------------------------------------------
>"Joseph M. Newcomer"
>> Generally, because there is no bounds checking done in pure C.  Also, it puts large
>> objects on the stack (there was some example of this posted here a few weeks ago, where
>> somebody had a local array of 17x17xmassive structure on the stack and was getting stack
>> overflows).  Overall, the advantage of CArray or std::vector is that the stack space
>> consumed is very small (a few bytes) and the data is always on the heap.
>
>> If you don't know how many you have in advance, a compile-time-constant array is a Really
>> Bad Idea.  Either you need to make it big enough to hold the absolute maximum you might
>> ever encounter (consuming massive stack space) or be prepared to deal with the fact that
>> the program is not adequate to the task because the array bounds, which must be set at
>> compile time, are too small.  CArray::SetAt will let you set any array element, and do
>> bounds checking, and CArray::SetAtGrow will expand the array to the specified size.
>> CArray::Add will expand it until memory exhausts.  There are analogous features for
>> std::vector.
>>
>> CArray<Inlet> InletA;
>> InletA.SetSize(10);
>> std::vector<Inlet> InletA;
>> InletA.resize(10);
>
Joseph M. Newcomer [MVP]
email: newcomer@flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
0
Joseph
5/7/2010 2:38:08 AM
Reply:

Similar Artilces:

Newsreader Question
I'm using Outlook Express for my newgroup reader. Everytime I open it, it tells me there are new newsgroups. How do I stop this? Thanks, James Tools | Options | General (tab) | Uncheck Notify... -- Jim Cone San Francisco, USA http://www.realezsites.com/bus/primitivesoftware "Zone" wrote in message I'm using Outlook Express for my newgroup reader. Everytime I open it, it tells me there are new newsgroups. How do I stop this? Thanks, James Thank you, Jim. "Jim Cone" <jim.coneXXX@rcn.comXXX> wrote in message news:e2$6swiRHHA.4088@TK2MSFTNG...

Link to a doc
I want to be able to have a document link to a contact. Every contact has an excel file that is saved on another server that contains there marks and I want to be able to attach the document. This would eliminate a step for my users. I also need to be able to open the document, post a mark and then be able to save and overwrite the original. Is this possible..... ...

Outlook views #7
Dear All, Sorry if this is a repeat...I can't find my message. If I am viewing email in all messages view. I selected one in which I was interested and then changed to conversation view in the hope that the selected email and associated emails would be highlighted. Alas no. Is there a way of doing this or of searching the conversations? I know I can search using find or advanced find but this does not group the results by conversation. Thanks, Danny ...

continious page nos while inserting pages from 2nd doc to 1st doc
i have 3 pages template in 2nd page from macro i am inserting file doc2 where bookmarks are there for i=0 to 2 Selection.GoTo What:=wdGoToSection, Which:=wdGoToLast Selection.MoveUp Unit:=wdLine, Count:=1 Selection.InsertBreak Type:=wdSectionBreakContinuous Selection.InsertFile Filename:=mstrTemplatePath, Range:="mtaLoop", ConfirmConversions:=False, Link:=False, Attachment:=False For Each frmfld In ActiveDocument.FormFields Select Case frmfld.Name Case "mta070" ...

Question about backend dbs
Is there any reason not to complie the backend as an .mde file to protect it before distributing it? Thanks in advance, Jim "Jim Evans" <jim@microsoftdiscussions.com> wrote in message news:uB$IEIHlKHA.2160@TK2MSFTNGP02.phx.gbl... > Is there any reason not to complie the backend as an .mde file to protect > it > before distributing it? There's no real reason to do that. MDE format doesn't protect the design of tables, and there wouldn't normally be anything in the back-end except tables and, maybe, a "Keep Out" form. ...

Budgeting question
Hello, I am confused on a budgeting issue. Say I have to pay 30 dollars every three months. Money breaks it down to say 10 dollars each month. When I pay the bill of 30 dollars on the 3rd month, Money says I am 20 dollars over I would like to have an envelope that would sum the values and on the third month would have 30 dollars in it. Then when I paid the bill, the enevlope would show a zero balance. I don't think Money can do this. Can someone explain this? Thanks Frank Hi Frank, This too frustrates me. The budget seems to force everything into monthly paments / earnings....

html docs to publisher docs
I have managed to delete the publisher file, but have the index and index_files. Is there a way I can reverse engineer these html files to the original publisher file? At the end of this article you will find a way to reconstruct your Publisher file from your web files: Common Sense Computing 101 aka "Why in the world would you lose your publisher file?" : http://msmvps.com/blogs/dbartosik/archive/2006/01/19/81461.aspx In the future, please post your web related questions in the web group and we will try to help you there: microsoft.public.publisher.webdesign DavidF "w...

Viewing different worksheets at the same time?
I was just wondering if it's at all possible to view different worksheets from one workbook at the same time. I got to thinking about this because you can split the view so that you can view different parts of one worksheet at the same time (dragging that little bar at the end of the scroll-bars). I'm currently having to create a load of links between two sheets withing a workbook and it would be so much easier if you could have both sheets on display at once. So, is this possible? Cheers, Rob -- Rob_T ------------------------------------------------------------------------ Rob_...

problems with hyperlinks to Xcel docs
I've gad sp2 installed recently and discovered that since: 1 - Hyperlinks to other Excel workbooks do not work, 2 - links to other office docs do work. I tried different method: Shift-Click or Ctrl-CLick but not luck so far. Any idea? Many TIA Joseph ...

repeat question....still looking for an answer
how are people handling the situation of no salesperson ID being transferred over from CRM to Great Plains on the customer card? Also how are people handling the situation of no tax schedule ID available in CRM? I'm told that my customer class ID in GP will have to be set up with a default tax id - THIS DOESN'T MAKE SENSE. Without a salesperson ID or a tax schedule ID it creates a lot more work for my accounting dept. they will have to go into every card in GP and add in the correct tax id and then they have to go to crm to find out who owns the account, go back to GP and ...

doc size limitations
Version: 2008 Operating System: Mac OS X 10.4 (Tiger) Processor: Power PC why are doc sizes limited to printer settings? i like to do floor plans and can never do an entire floor, just room by room. if i shrink things to 1/4 of the original dimensions, zooming in just makes things less sharp. is there any way to enlarge my doc size that i'm not seeing and save it as a custom setting? <br><br>e.g., i want to do six rooms, including bath, in a 32'X28' area. i can work with these dimensions with in a 1:12 ratio, but i can't customize the paper size to, say, 35...

Powershell / Search / Test-ExchangeSearch Ex 2007
Hi I have a user for who exchange search does not run. I have read the docs, and run the command Test-ExchangeSearch against his mailbox and it comes back as not enabled. The mailstore is enabled, and other accounts are ok, so it seems just to be the one user. I'm wondering if anybody knows the command to enable it for his account? TIA Dave On Tue, 20 Mar 2007 07:21:03 -0700, Dave Hood <DaveHood@discussions.microsoft.com> wrote: >Hi > >I have a user for who exchange search does not run. I have read the docs, >and run the command Test-ExchangeSearch against his m...

viewing page spreads
Creating a newsletter. Currently I can only view pages in two page spreads. Is there any way to view a full spread of multiple page newsletters? Rachel After managing to set up OE-QuoteFix on his new PC, Ed reads a message from Rachel <anonymous@discussions.microsoft.com>... > Creating a newsletter. Currently I can only view pages > in two page spreads. Is there any way to view a full > spread of multiple page newsletters? In Publisher 2002 and 2003 you can use the Print Preview to accompish this. -- Ed Bennett - MVP Microsoft Publisher http://www.mvps.org/the_nerd/ B...

Product Catalog Questions
Can someone please explain to me what the following fields are for when setting up a new product? -List price -Standrad Cost -Current cost Obviously one of these fields is for the sale price of the product... but which one? and what are the other fields fore? How do they affect the revenue forecasting? I bought the working with CRM Dynamics 3.0 book. However it has NOTHING about the product catalog section in it whatsoever. -Julius When you configure the price list for the products, the pricing rules can be based off these entries. -- Matt Parks MVP - Microsoft CRM "juls8...

Making a clean view
I just wrote a simple program and I would like to make it look clean an simple by removing all of the unecessary items from the menu. And b limiting the viewing area. There is a plug-in called Mortgage Genie o a website called moneycops.com. The program is free and it's a perfec example of what I want mine to look like. Can anyone help me out here -- Jua ----------------------------------------------------------------------- Juan's Profile: http://www.excelforum.com/member.php?action=getinfo&userid=688 View this thread: http://www.excelforum.com/showthread.php?threadid=26353 ...

Upgrade question
I am helping a friend upgrade from Vista to Windows 7. We have downloaded Windows 7 Home Premium from Windows Marketplace, including the iso image so we can put it on a DVD. I have cloned his hard drive. He is undecided as to whether to upgrade or to install from scratch. If we start from scratch, he has a lot of software to reinstall. So if we go the upgrade route and he doesn't like it, can we turn around and do a clean install? I'm mostly worried about activating it twice. I would try to avoid activating the upgrade until we were sure. I could always clone the original...

insert pub doc into pub doc- how?
Person sent me publisher document with page 3 & 4 blank. Then sent me pages 3 & 4 publisher document. I want to put pages 3 & 4 into the first publisher document. I have tried copy and paste but the copy does not copy whole page--maybe I have to do Group? Just got one box. File import said something about Word document-did not help! Thanks for suggestions! Susan Try Edit|Select All and then copy and paste. -- JoAnn Paules MVP Microsoft [Publisher] "Susan" <dsnsacree@msn.com> wrote in message news:OkfdhzK3EHA.4092@TK2MSFTNGP14.phx.gbl... > Person ...

XMLValidatingReader.Read() question
i am using an XMLValidatingReader in a manner similar to the following code example: string sReturn = ""; XmlValidatingReader oXML = new XmlValidatingReader(sXMLString, XmlNodeType.Document, null); oXML.Schemas.Add("", sSchemaURL); oXML.ValidationType = ValidationType.Schema; oXML.ValidationEventHandler += new ValidationEventHandler(ValidationHandler); while(oXML.Read()) { if (oXML.NodeType == XmlNodeType.Element) { sReturn = sReturn + "Name: " + oXML.LocalName + "<br>"; sReturn = sReturn + "Value: " + oXML.Value + "&l...

Excel 200 viewing option
I have win NT and office 2000. When I open multiple WORD documents, I get multiple buttons on my task bar for easier editing. But when I open multiple excel files, I get only one. How can I set it up like word? Thanks Tools / Options / View / Windows in Taskbar -- Regards Ken....................... Microsoft MVP - Excel Sys Spec - Win XP Pro / XL2K & XLXP ---------------------------------------------------------------------------- Attitude - A little thing that makes a BIG difference --------------------------------------------------------------...

Memory Question
I have not changed my soft wear. I did change my DSL provider. Now when I go to web sites and I put in the information sign or like mapquest it does not remember it. I have to keep putting it in. How can i get the computer to remember again? Um, not an Outlook problem. --� Milly Staples [MVP - Outlook] Post all replies to the group to keep the discussion intact. All unsolicited mail sent to my personal account will be deleted without reading. After furious head scratching, Memory Confused asked: | I have not changed my soft wear. I did change my DSL provider. Now | when I go to web si...

Print Question Newbie Please help
I am creating a spreadsheet that I need to print on 11x17 paper. My printer does not support 11x17 but I can send it to someone that does. How can I format my spreadsheet to use 11x17 so I can see all the cells etc. My guess is I need to install a print driver that supports 11x17 am I on the right track? If so can anyone suggest what I can use? Thanks Tom -------------------------------------------------------------------------------- I am using the free version of SPAMfighter for private users. It has removed 0 spam emails to date. Paying users do not have this message in their emails...

Doc/View question
I have reached a point where I need input on the following: For some time I have used MFC created apps with Doc/View and made my base view class as CFormView. It was convenient and quick, but I was putting all of my data variables in the ViewClass since that was where the class wizard put all my control variables and it seemed convenient in my ignorance. If I had to access files I used ofstream from within my view class. So now it has become apparent that I have things backwards (so it appears from what I read ) in that I should have been creating data variables in the CDocument...

Cell value/filename question...
I'm using forumulas like these to extract data from several differen files: =SUM('Coins\[coin value.xls]Group Costs'!$A$3). I have a colum that has all the filenames listed, but it's a huge hassle to go throug and change the formula each time I start a new row (keyword). What need is something like this: =SUM('Coins\[A3.xls]Group Costs'!$A$3). As you can see, I'm trying to get Excel to think that A3 really equal 'coin value'. I know this is possible with concatenation (it would tak a few extra steps) but I need all the data right in front of me as I' ...

Two part question
I know how to take an Email list in my Access database and to generate Emails from it. What I need now is a little more flexibility in that email, I need to use a voting button. Is that possible? ...

noob question
We are currently running sharepoint on a server2003 r2 machine. A recent addition of data took it over the 2gb limit so now we are unable to do anything with the site. Rather than battle with this problem it makes more sense to consider an additional server running SQL server to host it. Does this make sense? What would you recommend for the installation? At the moment it is a single server installation, running Exchange with approx 25 users Thanks in advance. If this is an inappropriate ng then doubtless someone will tell me Geoff ...