Another reason why OnPaint is protected because you don't have make a
method public unless you need to. If you make a method public then you
define a contract you are willing to support for a client of your object.
So you have to be choosy on what you want to expose to your client.
Main is an entry point to your application. You can have multiple mains in
your application code and you can tell the compiler through /main switch
which class contains the "main" method that serves as the application entry
point.
The default access of the "MyMethod" in your code is private. You could
possibly apply the same reasoning that the developer needs to explicitly
mention what is public because it is a contract that you have to adhere to.
By making it an explicit thing the developer is forced to make a conscious
choice.
--------------------
Reply-To: "p988" <p9**@hotmail.com>
From: "p988" <p9**@hotmail.com>
Subject: "protected" access specifier
Date: Mon, 10 Nov 2003 12:21:23 -0800
Lines: 39
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 5.00.2314.1300
X-MimeOLE: Produced By Microsoft MimeOLE V5.00.2314.1300
Message-ID: <#I**************@tk2msftngp13.phx.gbl>
Newsgroups: microsoft.public.dotnet.languages.csharp
NNTP-Posting-Host: 237.los-angeles10rh16rt.ca.dial-access.att.net
12.80.67.237Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!tk2msftn gp13.phx.gbl
Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.csharp:198168
X-Tomcat-NG: microsoft.public.dotnet.languages.csharp
using System;
using System.Windows.Forms;
using System.Drawing;
class MyForm : Form
{
MyForm ()
{
Text = "Windows Forms Demo";
}
protected override void OnPaint (PaintEventArgs e)
{
e.Graphics.DrawString ("Hello, world", Font,
new SolidBrush (Color.Black), ClientRectangle);
}
void MyMethod()
{
//...
}
static void Main ()
{
Application.Run (new MyForm ());
}
}
Why "protected" access specifier used for "OnPaint()" above?
Is this because that MyForm is potentially used as a parent class of
anotherlevel of deriative?
If MyForm class will be used as a parent class, how to handle the "static
void Main()" in its child classes?
What's the default access specifier for "MyMethod()" if it doesn't have
one?
Rakesh, EFT.
This posting is provided "AS IS" with no warranties, and confers no rights.
Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm