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;
}
}