473,420 Members | 1,603 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,420 software developers and data experts.

Petzold Assumed I Could Figure It Out -- I Am Stumped

Tom
First, my thanks in advance to those who can help explain away this
mystery line of code.

Petzold is a great teacher and I appreciate his work; however, his
explanation about an extra line of code needed when a class is
contained in a library vs the class being defined in a separate file
within the project falls short of my ability to understand.

Note: I've included the code (4 files) at the bottom, making this a
long winded posting; however, to emphasize the slight variance between
a library file and an included file ... I felt compelled to include
the code in it's entirety.

Restating the above >An event override within a locally defined
class does NOT require the base class method to be called; however,
when defining the SAME class within a DLL with the exact same
functionality ... you MUST call the base method.

protected override void OnClick(EventArgs args)
{
base.OnClick(args); // *** This is the extra line of code ***
MessageBox.Show(MessageBoxText, Text);
}

ref: "2005 Edition - Programming Microsoft Windows Forms - A
Streamlined Approach Using C#", by Charles Petzold, page #45.

In Petzold's words >"I've also added a statement to the OnClick
method to call the same method in the base class (which is Button).
Without this statement, a program couldn't attach a Click event
handler for MessageButton."

My confusion raises many questions:

1) Why does a class defined in a library file require this extra line
of code? Is there a problem within the JIT compilers and this is
simply a "trick" to make using a DLL work? (I doubt it. Programming is
very precise. I am just confused!!)

2) How do you know when to call a base class method? If you are
overriding the method ... why must the base method be called? If the
base method "does something" that you want to avoid or change by
overriding it ... does it make sense to have to call it?

3) Is a library file not simply a block of code that is reusable and
essentially the same as being included within the project? (Let's
exclude data argument marshaling between unmanaged DLL's and managed
code. A valid complexity for sure ... but not the focal point.)

4) Is the "public" access of the library's MessageButton class a
contributing factor?

5) What types of rules of understanding can be applied so one knows
when to call the base method?

Note: I've compiled and ran both programs below. They are solid and
behave the same. Also, all of the examples and solution files for the
referenced text can be found at:

http://www.microsoft.com/mspress/com...0-7356-2153-5/

The library file:

// The Library Contained MessageButton Class File: >>

//-------------------------------------------------
// MessageButtonLib.cs (c) 2005 by Charles Petzold
//-------------------------------------------------
using System;
using System.Drawing;
using System.Windows.Forms;

namespace Petzold.ProgrammingWindowsForms
{
public class MessageButton: Button
{
string strMessageBoxText;

public MessageButton()
{
Enabled = false;
}
public string MessageBoxText
{
set
{
strMessageBoxText = value;
Enabled = value != null && value.Length 0;
}
get
{
return strMessageBoxText;
}
}
protected override void OnClick(EventArgs args)
{
base.OnClick(args);
MessageBox.Show(MessageBoxText, Text);
}
}
}

// The File For Using the library file: >>

//----------------------------------------------------
// ProgramUsingLibrary.cs (c) 2005 by Charles Petzold
//----------------------------------------------------
using System;
using System.Drawing;
using System.Windows.Forms;
using Petzold.ProgrammingWindowsForms;

class ProgramUsingLibrary: Form
{
[STAThread]
public static void Main()
{
Application.EnableVisualStyles();
Application.Run(new ProgramUsingLibrary());
}
public ProgramUsingLibrary()
{
Text = "Program Using Library";

MessageButton msgbtn = new MessageButton();
msgbtn.Parent = this;
msgbtn.Text = "Calculate 10,000,000 digits of PI";
msgbtn.MessageBoxText = "This button is not yet implemented!";
msgbtn.Location = new Point(50, 50);
msgbtn.AutoSize = true;
}
}

================================================== ========================
================================================== ========================
================================================== ========================

// The "Locally Defined MessageButton Class"
// Within A Separate File: >>

//----------------------------------------------
// MessageButton.cs (c) 2005 by Charles Petzold
//----------------------------------------------
using System;
using System.Drawing;
using System.Windows.Forms;

class MessageButton: Button
{
string strMessageBoxText;

public MessageButton()
{
Enabled = false;
}
public string MessageBoxText
{
set
{
strMessageBoxText = value;
Enabled = value != null && value.Length 0;
}
get
{
return strMessageBoxText;
}
}
protected override void OnClick(EventArgs args)
{
MessageBox.Show(MessageBoxText, Text);
}
}
// File For Using the MessageButton Class: >>

//--------------------------------------------------
// MessageButtonDemo.cs (c) 2005 by Charles Petzold
//--------------------------------------------------
using System;
using System.Drawing;
using System.Windows.Forms;

class MessageButtonDemo: Form
{
[STAThread]
public static void Main()
{
Application.EnableVisualStyles();
Application.Run(new MessageButtonDemo());
}
public MessageButtonDemo()
{
Text = "MessageButton Demo";

MessageButton msgbtn = new MessageButton();
msgbtn.Parent = this;
msgbtn.Text = "Calculate 10,000,000 digits of PI";
msgbtn.MessageBoxText = "This button is not yet implemented!";
msgbtn.Location = new Point(50, 50);
msgbtn.AutoSize = true;
}
}
Nov 2 '07 #1
12 1610
Liz

"Tom" <Th********@earthlink.netwrote in message
news:bn********************************@4ax.com...
First, my thanks in advance to those who can help explain away this
mystery line of code.

Petzold is a great teacher and I appreciate his work; however, his
explanation about an extra line of code needed when a class is
contained in a library vs the class being defined in a separate file
within the project falls short of my ability to understand.

Actually, Petzold doesn't say everything you're attributing to him; the
point he makes is simply stated: "I've also added a statement to the OnClick
method to call the same method in the base class (which is Button). Without
this statement, a program couldn't attach a Click event handler for
MessageButton."

Far as I know, this is all correct and I don't know where he says that it's
ok to *not* call the base method "locally." It is indeed "ok" in either
case but there is a consequence which you may or may not care about in your
own code; but if you distribute your class in a DLL to someone else
expecting different behavior, it could be a problem, no?

It makes no difference whatsoever whether the code is in a library file or
an "included file"

Note: I've included the code (4 files) at the bottom, making this a
long winded posting; however, to emphasize the slight variance between
a library file and an included file ... I felt compelled to include
the code in it's entirety.

Restating the above >An event override within a locally defined
class does NOT require the base class method to be called; however,
when defining the SAME class within a DLL with the exact same
functionality ... you MUST call the base method.

protected override void OnClick(EventArgs args)
{
base.OnClick(args); // *** This is the extra line of code ***
MessageBox.Show(MessageBoxText, Text);
}

ref: "2005 Edition - Programming Microsoft Windows Forms - A
Streamlined Approach Using C#", by Charles Petzold, page #45.

In Petzold's words >"I've also added a statement to the OnClick
method to call the same method in the base class (which is Button).
Without this statement, a program couldn't attach a Click event
handler for MessageButton."

My confusion raises many questions:

1) Why does a class defined in a library file require this extra line
of code? Is there a problem within the JIT compilers and this is
simply a "trick" to make using a DLL work? (I doubt it. Programming is
very precise. I am just confused!!)

2) How do you know when to call a base class method? If you are
overriding the method ... why must the base method be called? If the
base method "does something" that you want to avoid or change by
overriding it ... does it make sense to have to call it?

3) Is a library file not simply a block of code that is reusable and
essentially the same as being included within the project? (Let's
exclude data argument marshaling between unmanaged DLL's and managed
code. A valid complexity for sure ... but not the focal point.)

4) Is the "public" access of the library's MessageButton class a
contributing factor?

5) What types of rules of understanding can be applied so one knows
when to call the base method?

Note: I've compiled and ran both programs below. They are solid and
behave the same. Also, all of the examples and solution files for the
referenced text can be found at:

http://www.microsoft.com/mspress/com...0-7356-2153-5/

The library file:

// The Library Contained MessageButton Class File: >>

//-------------------------------------------------
// MessageButtonLib.cs (c) 2005 by Charles Petzold
//-------------------------------------------------
using System;
using System.Drawing;
using System.Windows.Forms;

namespace Petzold.ProgrammingWindowsForms
{
public class MessageButton: Button
{
string strMessageBoxText;

public MessageButton()
{
Enabled = false;
}
public string MessageBoxText
{
set
{
strMessageBoxText = value;
Enabled = value != null && value.Length 0;
}
get
{
return strMessageBoxText;
}
}
protected override void OnClick(EventArgs args)
{
base.OnClick(args);
MessageBox.Show(MessageBoxText, Text);
}
}
}

// The File For Using the library file: >>

//----------------------------------------------------
// ProgramUsingLibrary.cs (c) 2005 by Charles Petzold
//----------------------------------------------------
using System;
using System.Drawing;
using System.Windows.Forms;
using Petzold.ProgrammingWindowsForms;

class ProgramUsingLibrary: Form
{
[STAThread]
public static void Main()
{
Application.EnableVisualStyles();
Application.Run(new ProgramUsingLibrary());
}
public ProgramUsingLibrary()
{
Text = "Program Using Library";

MessageButton msgbtn = new MessageButton();
msgbtn.Parent = this;
msgbtn.Text = "Calculate 10,000,000 digits of PI";
msgbtn.MessageBoxText = "This button is not yet implemented!";
msgbtn.Location = new Point(50, 50);
msgbtn.AutoSize = true;
}
}

================================================== ========================
================================================== ========================
================================================== ========================

// The "Locally Defined MessageButton Class"
// Within A Separate File: >>

//----------------------------------------------
// MessageButton.cs (c) 2005 by Charles Petzold
//----------------------------------------------
using System;
using System.Drawing;
using System.Windows.Forms;

class MessageButton: Button
{
string strMessageBoxText;

public MessageButton()
{
Enabled = false;
}
public string MessageBoxText
{
set
{
strMessageBoxText = value;
Enabled = value != null && value.Length 0;
}
get
{
return strMessageBoxText;
}
}
protected override void OnClick(EventArgs args)
{
MessageBox.Show(MessageBoxText, Text);
}
}
// File For Using the MessageButton Class: >>

//--------------------------------------------------
// MessageButtonDemo.cs (c) 2005 by Charles Petzold
//--------------------------------------------------
using System;
using System.Drawing;
using System.Windows.Forms;

class MessageButtonDemo: Form
{
[STAThread]
public static void Main()
{
Application.EnableVisualStyles();
Application.Run(new MessageButtonDemo());
}
public MessageButtonDemo()
{
Text = "MessageButton Demo";

MessageButton msgbtn = new MessageButton();
msgbtn.Parent = this;
msgbtn.Text = "Calculate 10,000,000 digits of PI";
msgbtn.MessageBoxText = "This button is not yet implemented!";
msgbtn.Location = new Point(50, 50);
msgbtn.AutoSize = true;
}
}


Nov 2 '07 #2
Liz

"Tom" <Th********@earthlink.netwrote in message
news:bn********************************@4ax.com...
First, my thanks in advance to those who can help explain away this
mystery line of code.

Petzold is a great teacher and I appreciate his work; however, his
explanation about an extra line of code needed when a class is
contained in a library vs the class being defined in a separate file
within the project falls short of my ability to understand.
replace the ProgramUsingLibrary.cs file with the code below (which does
nothing more than subscribe to the Click event), then comment out the
statement:

base.OnClick(args);

in MessageButtonLib.cs

you should see the problem with not calling the base method.
//----------------------------------------------------
// ProgramUsingLibrary.cs (c) 2005 by Charles Petzold
//----------------------------------------------------
using System;
using System.Drawing;
using System.Windows.Forms;
using Petzold.ProgrammingWindowsForms;

class ProgramUsingLibrary: Form
{
[STAThread]
public static void Main()
{
Application.EnableVisualStyles();
Application.Run(new ProgramUsingLibrary());
}
public ProgramUsingLibrary()
{
Text = "Program Using Library";

MessageButton msgbtn = new MessageButton();
msgbtn.Parent = this;
msgbtn.Text = "Calculate 10,000,000 digits of PI";
msgbtn.MessageBoxText = "This button is not yet implemented!";
msgbtn.Location = new Point(50, 50);
msgbtn.AutoSize = true;

msgbtn.Click += new EventHandler(msgbtn_Click);
}

void msgbtn_Click(object sender, EventArgs e)
{
MessageBox.Show("click");
}
}
Note: I've included the code (4 files) at the bottom, making this a
long winded posting; however, to emphasize the slight variance between
a library file and an included file ... I felt compelled to include
the code in it's entirety.

Restating the above >An event override within a locally defined
class does NOT require the base class method to be called; however,
when defining the SAME class within a DLL with the exact same
functionality ... you MUST call the base method.

protected override void OnClick(EventArgs args)
{
base.OnClick(args); // *** This is the extra line of code ***
MessageBox.Show(MessageBoxText, Text);
}

ref: "2005 Edition - Programming Microsoft Windows Forms - A
Streamlined Approach Using C#", by Charles Petzold, page #45.

In Petzold's words >"I've also added a statement to the OnClick
method to call the same method in the base class (which is Button).
Without this statement, a program couldn't attach a Click event
handler for MessageButton."

My confusion raises many questions:

1) Why does a class defined in a library file require this extra line
of code? Is there a problem within the JIT compilers and this is
simply a "trick" to make using a DLL work? (I doubt it. Programming is
very precise. I am just confused!!)

2) How do you know when to call a base class method? If you are
overriding the method ... why must the base method be called? If the
base method "does something" that you want to avoid or change by
overriding it ... does it make sense to have to call it?

3) Is a library file not simply a block of code that is reusable and
essentially the same as being included within the project? (Let's
exclude data argument marshaling between unmanaged DLL's and managed
code. A valid complexity for sure ... but not the focal point.)

4) Is the "public" access of the library's MessageButton class a
contributing factor?

5) What types of rules of understanding can be applied so one knows
when to call the base method?

Note: I've compiled and ran both programs below. They are solid and
behave the same. Also, all of the examples and solution files for the
referenced text can be found at:

http://www.microsoft.com/mspress/com...0-7356-2153-5/

The library file:

// The Library Contained MessageButton Class File: >>

//-------------------------------------------------
// MessageButtonLib.cs (c) 2005 by Charles Petzold
//-------------------------------------------------
using System;
using System.Drawing;
using System.Windows.Forms;

namespace Petzold.ProgrammingWindowsForms
{
public class MessageButton: Button
{
string strMessageBoxText;

public MessageButton()
{
Enabled = false;
}
public string MessageBoxText
{
set
{
strMessageBoxText = value;
Enabled = value != null && value.Length 0;
}
get
{
return strMessageBoxText;
}
}
protected override void OnClick(EventArgs args)
{
base.OnClick(args);
MessageBox.Show(MessageBoxText, Text);
}
}
}

// The File For Using the library file: >>

//----------------------------------------------------
// ProgramUsingLibrary.cs (c) 2005 by Charles Petzold
//----------------------------------------------------
using System;
using System.Drawing;
using System.Windows.Forms;
using Petzold.ProgrammingWindowsForms;

class ProgramUsingLibrary: Form
{
[STAThread]
public static void Main()
{
Application.EnableVisualStyles();
Application.Run(new ProgramUsingLibrary());
}
public ProgramUsingLibrary()
{
Text = "Program Using Library";

MessageButton msgbtn = new MessageButton();
msgbtn.Parent = this;
msgbtn.Text = "Calculate 10,000,000 digits of PI";
msgbtn.MessageBoxText = "This button is not yet implemented!";
msgbtn.Location = new Point(50, 50);
msgbtn.AutoSize = true;
}
}

================================================== ========================
================================================== ========================
================================================== ========================

// The "Locally Defined MessageButton Class"
// Within A Separate File: >>

//----------------------------------------------
// MessageButton.cs (c) 2005 by Charles Petzold
//----------------------------------------------
using System;
using System.Drawing;
using System.Windows.Forms;

class MessageButton: Button
{
string strMessageBoxText;

public MessageButton()
{
Enabled = false;
}
public string MessageBoxText
{
set
{
strMessageBoxText = value;
Enabled = value != null && value.Length 0;
}
get
{
return strMessageBoxText;
}
}
protected override void OnClick(EventArgs args)
{
MessageBox.Show(MessageBoxText, Text);
}
}
// File For Using the MessageButton Class: >>

//--------------------------------------------------
// MessageButtonDemo.cs (c) 2005 by Charles Petzold
//--------------------------------------------------
using System;
using System.Drawing;
using System.Windows.Forms;

class MessageButtonDemo: Form
{
[STAThread]
public static void Main()
{
Application.EnableVisualStyles();
Application.Run(new MessageButtonDemo());
}
public MessageButtonDemo()
{
Text = "MessageButton Demo";

MessageButton msgbtn = new MessageButton();
msgbtn.Parent = this;
msgbtn.Text = "Calculate 10,000,000 digits of PI";
msgbtn.MessageBoxText = "This button is not yet implemented!";
msgbtn.Location = new Point(50, 50);
msgbtn.AutoSize = true;
}
}


Nov 2 '07 #3
Tom
Thanks Liz --

I did comment out the "mystery" line and I see no difference in
behavior. The reason I attribute Petzold for it to be OK "not" to call
the base method in the locally defined file >that line of code is
not there. He goes out of his way to say that he has added it in the
DLL version and that without it there is a problem ... but I do not
see the resulting problems in the code behavior.

I notice in some of the other examples within the book that similar
calls to the base method are present in protected overrides.

To me it seems the inherited methods should be avoided and only the
overridden code be executed? It seems illogical to call a method who's
behavior is not desired? Perhaps if the override is simply "adding" to
the base method ... then you call the base and follow it with your
additional coding?

I remain confused ... but very much appreciate your comments.

The fact I see no behavior changes after commenting out that line
makes me wonder if the JIT compiler might have had a bug in it that
has now been fixed? Petzold does discuss on page xiii that the
examples were based on a specific pre-release version: August 2005
Community Technical Review.

I'm trying to scrutinize Petzold's work and understand it fully. He
does leap at times and thus forces this student to dig a bit to
understand the examples. Painful, but in the long run the journey also
reveals additional tidbits. It is just this particular line of code
and the "reasoning" behind it has me stumped.

Thanks again.

-- Tom

On Fri, 2 Nov 2007 18:00:08 -0500, "Liz" <li*@tiredofspam.comwrote:
>
"Tom" <Th********@earthlink.netwrote in message
news:bn********************************@4ax.com.. .
>First, my thanks in advance to those who can help explain away this
mystery line of code.

Petzold is a great teacher and I appreciate his work; however, his
explanation about an extra line of code needed when a class is
contained in a library vs the class being defined in a separate file
within the project falls short of my ability to understand.

replace the ProgramUsingLibrary.cs file with the code below (which does
nothing more than subscribe to the Click event), then comment out the
statement:

base.OnClick(args);

in MessageButtonLib.cs

you should see the problem with not calling the base method.
//----------------------------------------------------
// ProgramUsingLibrary.cs (c) 2005 by Charles Petzold
//----------------------------------------------------
using System;
using System.Drawing;
using System.Windows.Forms;
using Petzold.ProgrammingWindowsForms;

class ProgramUsingLibrary: Form
{
[STAThread]
public static void Main()
{
Application.EnableVisualStyles();
Application.Run(new ProgramUsingLibrary());
}
public ProgramUsingLibrary()
{
Text = "Program Using Library";

MessageButton msgbtn = new MessageButton();
msgbtn.Parent = this;
msgbtn.Text = "Calculate 10,000,000 digits of PI";
msgbtn.MessageBoxText = "This button is not yet implemented!";
msgbtn.Location = new Point(50, 50);
msgbtn.AutoSize = true;

msgbtn.Click += new EventHandler(msgbtn_Click);
}

void msgbtn_Click(object sender, EventArgs e)
{
MessageBox.Show("click");
}
}
>Note: I've included the code (4 files) at the bottom, making this a
long winded posting; however, to emphasize the slight variance between
a library file and an included file ... I felt compelled to include
the code in it's entirety.

Restating the above >An event override within a locally defined
class does NOT require the base class method to be called; however,
when defining the SAME class within a DLL with the exact same
functionality ... you MUST call the base method.

protected override void OnClick(EventArgs args)
{
base.OnClick(args); // *** This is the extra line of code ***
MessageBox.Show(MessageBoxText, Text);
}

ref: "2005 Edition - Programming Microsoft Windows Forms - A
Streamlined Approach Using C#", by Charles Petzold, page #45.

In Petzold's words >"I've also added a statement to the OnClick
method to call the same method in the base class (which is Button).
Without this statement, a program couldn't attach a Click event
handler for MessageButton."

My confusion raises many questions:

1) Why does a class defined in a library file require this extra line
of code? Is there a problem within the JIT compilers and this is
simply a "trick" to make using a DLL work? (I doubt it. Programming is
very precise. I am just confused!!)

2) How do you know when to call a base class method? If you are
overriding the method ... why must the base method be called? If the
base method "does something" that you want to avoid or change by
overriding it ... does it make sense to have to call it?

3) Is a library file not simply a block of code that is reusable and
essentially the same as being included within the project? (Let's
exclude data argument marshaling between unmanaged DLL's and managed
code. A valid complexity for sure ... but not the focal point.)

4) Is the "public" access of the library's MessageButton class a
contributing factor?

5) What types of rules of understanding can be applied so one knows
when to call the base method?

Note: I've compiled and ran both programs below. They are solid and
behave the same. Also, all of the examples and solution files for the
referenced text can be found at:

http://www.microsoft.com/mspress/com...0-7356-2153-5/

The library file:

// The Library Contained MessageButton Class File: >>

//-------------------------------------------------
// MessageButtonLib.cs (c) 2005 by Charles Petzold
//-------------------------------------------------
using System;
using System.Drawing;
using System.Windows.Forms;

namespace Petzold.ProgrammingWindowsForms
{
public class MessageButton: Button
{
string strMessageBoxText;

public MessageButton()
{
Enabled = false;
}
public string MessageBoxText
{
set
{
strMessageBoxText = value;
Enabled = value != null && value.Length 0;
}
get
{
return strMessageBoxText;
}
}
protected override void OnClick(EventArgs args)
{
base.OnClick(args);
MessageBox.Show(MessageBoxText, Text);
}
}
}

// The File For Using the library file: >>

//----------------------------------------------------
// ProgramUsingLibrary.cs (c) 2005 by Charles Petzold
//----------------------------------------------------
using System;
using System.Drawing;
using System.Windows.Forms;
using Petzold.ProgrammingWindowsForms;

class ProgramUsingLibrary: Form
{
[STAThread]
public static void Main()
{
Application.EnableVisualStyles();
Application.Run(new ProgramUsingLibrary());
}
public ProgramUsingLibrary()
{
Text = "Program Using Library";

MessageButton msgbtn = new MessageButton();
msgbtn.Parent = this;
msgbtn.Text = "Calculate 10,000,000 digits of PI";
msgbtn.MessageBoxText = "This button is not yet implemented!";
msgbtn.Location = new Point(50, 50);
msgbtn.AutoSize = true;
}
}

================================================= =========================
================================================= =========================
================================================= =========================

// The "Locally Defined MessageButton Class"
// Within A Separate File: >>

//----------------------------------------------
// MessageButton.cs (c) 2005 by Charles Petzold
//----------------------------------------------
using System;
using System.Drawing;
using System.Windows.Forms;

class MessageButton: Button
{
string strMessageBoxText;

public MessageButton()
{
Enabled = false;
}
public string MessageBoxText
{
set
{
strMessageBoxText = value;
Enabled = value != null && value.Length 0;
}
get
{
return strMessageBoxText;
}
}
protected override void OnClick(EventArgs args)
{
MessageBox.Show(MessageBoxText, Text);
}
}
// File For Using the MessageButton Class: >>

//--------------------------------------------------
// MessageButtonDemo.cs (c) 2005 by Charles Petzold
//--------------------------------------------------
using System;
using System.Drawing;
using System.Windows.Forms;

class MessageButtonDemo: Form
{
[STAThread]
public static void Main()
{
Application.EnableVisualStyles();
Application.Run(new MessageButtonDemo());
}
public MessageButtonDemo()
{
Text = "MessageButton Demo";

MessageButton msgbtn = new MessageButton();
msgbtn.Parent = this;
msgbtn.Text = "Calculate 10,000,000 digits of PI";
msgbtn.MessageBoxText = "This button is not yet implemented!";
msgbtn.Location = new Point(50, 50);
msgbtn.AutoSize = true;
}
}

Nov 3 '07 #4
Liz

"Tom" <Th********@earthlink.netwrote in message
news:3u********************************@4ax.com...
Thanks Liz --

I did comment out the "mystery" line and I see no difference in
behavior.
of course there is, Tom ... as Petzold points out, without the base method
call, you cannot attach a Click event handler (well, you can, but it won't
function) .... that should be clear from commenting/uncommenting the
base.OnClick(args) statement and running the code.
The reason I attribute Petzold for it to be OK "not" to call
the base method in the locally defined file >that line of code is
not there. He goes out of his way to say that he has added it in the
DLL version and that without it there is a problem ... but I do not
see the resulting problems in the code behavior.
I'm not sure I'd characterize it as "going out of his way" nor do I think
he's differentiating DLLs on this point; the inclusion of the call in the
DLL seems to more incidental than otherwise ... the section is a brief
description/tutorial on creating DLLs; note that he points out that it is
"courteous" to use a namespace in your DLL and essential to declare classes
as public; now he does not say that you *must* call the base method; he
merely points out the effect of not doing so: if you want the user of your
DLL to able to make the full and best use of it, you call the base method.
I don't consider this to be gospel; it's just good general counsel which
can be ignored where appropriate.
To me it seems the inherited methods should be avoided and only the
overridden code be executed? It seems illogical to call a method who's
behavior is not desired? Perhaps if the override is simply "adding" to
the base method ... then you call the base and follow it with your
additional coding?
I understand you on this one and I think the answer is "it depends" on the
code you're writing and the way you intend/expect it to be used; I think it
might have been useful to have some examples from the maestro illustrating
when/why you want to call base methods and when/why you might not want to.
Surely you've noticed this is a relatively short book ... much is left
unsaid and left to the reader to ponder and figure out .. the approach has
its pros and cons but all overall I think it's useful and well-written.
I remain confused ... but very much appreciate your comments.
why don't you drop Charles an email and see if he has any comment on the
matter?

Nov 3 '07 #5
Tom
Liz --

I agree with you fully on the "maestro" tag. I don't feel qualified to
approach Petzold directly out of respect for his time and respect for
his invaluable works.

The forms book is condensed and I like it that way. I am among those
who got lost in the verbose coverage of graphics in other books. A
little struggle and challenge is needed to engage the learning process
and I am finding the forms book to be a good balance for me.

Petzold explains the extra overhead that VS generates very well and
when namespaces and the extra solution layer are not really needed for
the tiny learning programs. I am a big fan of concise code. There are
few callings higher than teaching ... the writing challenge to be
technically perfect, concise, thorough, and remain readable is way out
of my grasp.

I have been and remain a fan of Petzold's published works.

Just in this specific case ... the need for the base class method call
escapes me. I want to understand the logic fully. There is a reason
for the base method inclusion. That reason may not be required in this
case; however, if I can understand when to use it properly ... I will
have benefitted more so than simply moving on.

To add to my confusion is another program within the same book:
Chapter 3, ColorFill.

There, you can find a similar base method call in the following:

protected override void OnFontChanged(EventArgs args)
{
base.OnFontChanged(args); // << similar base method call ?????
Padding = new Padding(Padding.Left, yDpi / 10 + Font.Height,
Padding.Right, Padding.Bottom);
}

Again, I run the program with and without the base method call and on
my system the resulting behavior is the same. Compiles correctly and
runs identically.

Thus I remain convinced there are times when you should call the base
method and I would like to understand the logic to this level and
beyond someday. For these specific cases (and others within the same
book) ... what purpose is served calling the base method?

-- Tom

On Sat, 3 Nov 2007 02:56:20 -0500, "Liz" <li*@tiredofspam.comwrote:
>
"Tom" <Th********@earthlink.netwrote in message
news:3u********************************@4ax.com.. .
>Thanks Liz --

I did comment out the "mystery" line and I see no difference in
behavior.

of course there is, Tom ... as Petzold points out, without the base method
call, you cannot attach a Click event handler (well, you can, but it won't
function) .... that should be clear from commenting/uncommenting the
base.OnClick(args) statement and running the code.
>The reason I attribute Petzold for it to be OK "not" to call
the base method in the locally defined file >that line of code is
not there. He goes out of his way to say that he has added it in the
DLL version and that without it there is a problem ... but I do not
see the resulting problems in the code behavior.

I'm not sure I'd characterize it as "going out of his way" nor do I think
he's differentiating DLLs on this point; the inclusion of the call in the
DLL seems to more incidental than otherwise ... the section is a brief
description/tutorial on creating DLLs; note that he points out that it is
"courteous" to use a namespace in your DLL and essential to declare classes
as public; now he does not say that you *must* call the base method; he
merely points out the effect of not doing so: if you want the user of your
DLL to able to make the full and best use of it, you call the base method.
I don't consider this to be gospel; it's just good general counsel which
can be ignored where appropriate.
>To me it seems the inherited methods should be avoided and only the
overridden code be executed? It seems illogical to call a method who's
behavior is not desired? Perhaps if the override is simply "adding" to
the base method ... then you call the base and follow it with your
additional coding?

I understand you on this one and I think the answer is "it depends" on the
code you're writing and the way you intend/expect it to be used; I think it
might have been useful to have some examples from the maestro illustrating
when/why you want to call base methods and when/why you might not want to.
Surely you've noticed this is a relatively short book ... much is left
unsaid and left to the reader to ponder and figure out .. the approach has
its pros and cons but all overall I think it's useful and well-written.
>I remain confused ... but very much appreciate your comments.

why don't you drop Charles an email and see if he has any comment on the
matter?

Nov 3 '07 #6
Liz

"Tom" <Th********@earthlink.netwrote in message
news:2m********************************@4ax.com...
Liz --
I'm not sure where we're not communicating here; below is the program that
needs to be modified to see the difference in behavior; I'm clearly marking
the lines of code I added to expose the difference ... perhaps you didn't
add them to your code? If you comment out the statement base.OnClick(args)
you will not get execution of the code in the method "msgbtn_Click" ... if
you un-comment the statement base.OnClick(args) you WILL get execution of
the method "msgbtn_Click"

//----------------------------------------------------
// ProgramUsingLibrary.cs (c) 2005 by Charles Petzold
//----------------------------------------------------
using System;
using System.Drawing;
using System.Windows.Forms;
using Petzold.ProgrammingWindowsForms;

class ProgramUsingLibrary: Form
{
[STAThread]
public static void Main()
{
Application.EnableVisualStyles();
Application.Run(new ProgramUsingLibrary());
}
public ProgramUsingLibrary()
{
Text = "Program Using Library";

MessageButton msgbtn = new MessageButton();
msgbtn.Parent = this;
msgbtn.Text = "Calculate 10,000,000 digits of PI";
msgbtn.MessageBoxText = "This button is not yet implemented!";
msgbtn.Location = new Point(50, 50);
msgbtn.AutoSize = true;

msgbtn.Click += new EventHandler(msgbtn_Click); // *** ADDED THIS
LINE
}

void msgbtn_Click(object sender, EventArgs e) // *** ADDED THIS
LINE
{ // *** ADDED THIS
LINE
MessageBox.Show("click"); // *** ADDED THIS
LINE
} // *** ADDED THIS
LINE
}
Just in this specific case ... the need for the base class method call
escapes me. I want to understand the logic fully. There is a reason
for the base method inclusion. That reason may not be required in this
case; however, if I can understand when to use it properly ... I will
have benefitted more so than simply moving on.
let's consider a (silly) example: you're designing a UI control -- a
button -- which when clicked will call a block of code which will dispatch a
"send in the troops" message to the Joint Chiefs of Staff by email. Your
control will be used by hundreds of programmers in the military. When the
button is clicked, the communications behavior is hard-coded and immutable:
the email is sent. However, you have also been instructed to allow the
programmers to repsond to the button click in other ways ... perhaps to
display a message, "the button was clicked" .... if you override OnClick
without calling base.OnClick, the latter opportunity is foreclosed; the
programmer is limited to the email being sent

public class SendTroopsButton : Button
{

protected override void OnClick(EventArgs args)
{
base.OnClick(args);
dispatchSendTroopsEmail();
}

}

if you use this class:
{
...
SendTroopsButton troopsButton = new SendTroopsButton();
troopsButton.OnClick += new EventHandler(displayMessage); // displays
message on form that button was clicked
...
}

displayMessage will ONLY be called when the button is clicked if you
included the base.OnClick(args) statement in your SendTroops class;
dispatchSendTroopsEmail() will always be called when the button is clicked
.... that's why you might need a base call
I agree with you fully on the "maestro" tag. I don't feel qualified to
approach Petzold directly out of respect for his time and respect for
his invaluable works.
he invites email ... but does not promise a reply ...

I don't know if you're focused on looking for a difference in behavior
between hosting the class in a DLL vs locally but that will not make a
difference ... the difference is in calling base.Method vs not calling it
and to see the difference you must attempt to handle the event independently
of the behavior specified in the override ... clearly there will be cases
where whatever executes in the override will cover all of the behavior
required and where you don't want independent handling of the event, in
which case of course you would not use a base call

I think Petzold's implied generic suggestion was that calling base is a good
idea so you don't foreclose handling the event .. he just didn't spell out
all the rationale.

L

Nov 3 '07 #7

"Tom" <Th********@earthlink.netwrote in message
news:2m********************************@4ax.com...
>
To add to my confusion is another program within the same book:
Chapter 3, ColorFill.

There, you can find a similar base method call in the following:

protected override void OnFontChanged(EventArgs args)
{
base.OnFontChanged(args); // << similar base method call ?????
Padding = new Padding(Padding.Left, yDpi / 10 + Font.Height,
Padding.Right, Padding.Bottom);
}

Again, I run the program with and without the base method call and on
my system the resulting behavior is the same. Compiles correctly and
runs identically.
Using this example:

I don't have any of Charles' books so I can't refer to any examples in it,
but I very much doubt that the behaviour is the same when you omit the base
method call.

Changing the Font will cause a Paint event to be raised, but in this
specific example I assume that changing Padding will also cause a Paint
event.

Font is an ambient property. If you don't explicitly set it, then a control
will use it's Parent controls Font value.
When the parent font is changed the child controls will also be updated.
This may mean a resize of the child control as well as a repaint.
If you do not call the base.OnFontChanged() method then, in this case,
setting the Padding may well cause a repaint of the child controls, but it
will not cause them to be resized. If the control does not have any child
controls, then there may not be any advantage in calling the base method.

Some On* methods do not do anything other than raise an event. If you want
the event to be raised then either call the base method, or raise the method
yourself.

Unless you have intimate knowledge of the class and what it does in it's
base method, then it is best to call the method as not doing so may cause
other undesired behaviour which may go unnoticed until you see a specific
set of conditions.

--
Mick Doherty
http://www.dotnetrix.co.uk/nothing.html
Nov 4 '07 #8
Tom
The lights are now on!!

Thank you Liz & Mick.

Petzold was greatly enhancing the flexibility of the DLL contained
class by including a line of code that in the simplest of cases is not
needed.

I was too focused on: "What does this line of code do?" in this
*specific* case!

Liz -- Your example was perfect. It was my haste to understand and
respond back here that caused my confusion. I simply(and dumbly)
commented out the base class call in the override and ran it. Because
the base method in this particular program does nothing ... there was
no change in behavior. Your changing the nothing to something reveals
Petzold's point!!

The library user often does not have the source of the DLL and thus
can not modify the override function. Enhancements to the base methods
occur at the base level and require the override to execute them.

I had been frozen in confusion and now can move forward.

Thanks !!!!!!!!!!

-- Tom
Nov 4 '07 #9
<snip />

Petzold? I'm wondering if anybody can articulate what Simonyi is doing.
Nov 6 '07 #10
Liz

"clintonG" <no****@nowhere.comwrote in message
news:O6**************@TK2MSFTNGP06.phx.gbl...
<snip />
Petzold? I'm wondering if anybody can articulate what Simonyi is doing.

tell us why we care ... last I heard he was hanging out with Martha Stewart
and she was decorating his space ship or something ...
Nov 6 '07 #11
Tom
Now that made me laugh !!

I should have but didn't recognize the name. I am not a computer
historian and that name is not on any computer texts I own ... so
Yahoo search revealed: simonyiCreatedHungarianNotation and was
instrumental in the Word and Excel development. Very impressive. Major
rich. Space tourist and endowed a couple of scientific professorship
programs.

Now he is all about "Intentional" programming.

http://www.edge.org/digerati/simonyi/simonyi_p2.html

From the above link >>

SIMONYI: I have been working on what we call "intentional
programming." It's very exciting. It has to do with professional
programming, so it's kind of hard to get into the details. It also
relates to the work of evolutionary biologist Richard Dawkins in a
fairly direct way. We are trying to create an ecology of abstractions.
Abstraction is really the most powerful tool that we have in thinking
about problems. An abstraction is a single thing, yet if it is a good
one, it can have many applications, even an infinite number of them.
So an abstraction may be looked at from one side as a compression of
many instances into one generality or from the other side as a special
purpose power tool that yields the solution for many problems. If one
could attach a dollar sign to this power, the economies would be
amazing: rivaling that of chips or application software itself.

That last paragraph sentence above deserves reading a few times.

I wish him luck!! Feed that algorithm 'how to conquer cold fusion' and
solve the world's energy requirements. Then cure all the major
illnesses. Then solve world wide racial issues. Hmmm ... does religion
promote racism? Then perhaps a transporter beam would be fun. What
next?

On Mon, 5 Nov 2007 18:31:51 -0600, "Liz" <li*@tiredofspam.comwrote:
>
"clintonG" <no****@nowhere.comwrote in message
news:O6**************@TK2MSFTNGP06.phx.gbl...
><snip />
>Petzold? I'm wondering if anybody can articulate what Simonyi is doing.


tell us why we care ... last I heard he was hanging out with Martha Stewart
and she was decorating his space ship or something ...
Nov 7 '07 #12
Liz

"Tom" <Th********@earthlink.netwrote in message
news:ii********************************@4ax.com...
Now he is all about "Intentional" programming.
the entire MS culture is now about "intentional engineering" ... which makes
me wonder what it was previously about; was Windows accidental?
SIMONYI: I have been working on what we call "intentional
programming." It's very exciting. It has to do with professional
....
So an abstraction may be looked at from one side as a compression of
many instances into one generality or from the other side as a special
purpose power tool that yields the solution for many problems. If one
could attach a dollar sign to this power, the economies would be
amazing: rivaling that of chips or application software itself.
this is what can happen when your net worth exceeds $1 billion ... truly
frightening ... I don't know what Martha sees in the man ... what do they
talk about? intentional Christmas ornaments?

Nov 7 '07 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Michael O'Keeffe | last post by:
There was an interesting talk by Charles Petzold, who wrote the bible on the Wind32 API, on Visual Studio, and IDEs in general, and their disadvantages as far as learning (he also teaches...
5
by: Victor | last post by:
In his book "Programming Microsoft Windows with C#", page 42, Petzold deals with the case of a project with multiple Mains. He explains that when compiling in the command line the argument /main...
1
by: Terry Olsen | last post by:
I use the following code to create an XML string: Private Function CreatePacket(ByVal pt As PacketType) As String Dim xmlDoc As TextWriter = New StringWriter Dim xmlWriter As New...
3
by: Sumit RAJAN | last post by:
In the second Chapter of Charles Petzold's "Programming Windows", 5th Edition, we have the following listing: /*----------------------------------------------------- SCRNSIZE.C -- Displays...
3
by: Martijn Mulder | last post by:
What book on .NET, preferrably with code and examples in C#, can be thought of as the equivalent of the influencial 'Programming Windows' from Charles Petzold?
1
by: slamtart | last post by:
Hi everyone, Stumped for awhile with this one. Not sure what to do next. Basically, I have a laptop computer which I use for work. Prior to around May of this year, there's no trick to...
2
by: Steve K. | last post by:
I am in over my head I think, I've got my brain running in circles trying to figure this out. I'm using remoting with a client and a server. I need the server to raise events on the client and...
34
by: raylopez99 | last post by:
What is the state of C#? Somebody in a Linux advocacy newsgroup implied it has saturated (leveled off in growth). Note the 'hard code' pre-Wizards coding wizard Charles Petzold does C# only now....
0
by: TheOne | last post by:
I'm reading Petzold's "APPLICATIONS=CODE+MARKUP". In chapter 1, sample csharp code shows how an application inherited class overrides OnSessionEnding handler. Code looks like following ///...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.