"protected" access specifier

  • Thread starter Thread starter p988
  • Start date Start date
P

p988

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 another
level 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?
 
Why "protected" access specifier used for "OnPaint()" above?

Because that's how the OnPaint method is originally defined in the
base class. It makes it accessible to the class itself and its base
classes, but not to external classes.

Is this because that MyForm is potentially used as a parent class of another
level of deriative?
No


If MyForm class will be used as a parent class, how to handle the "static
void Main()" in its child classes?

You shouldn't have to "handle" it in any special way. Derived classes
can't override static methods, so they aren't affected by inheritance.

What's the default access specifier for "MyMethod()" if it doesn't have one?

private



Mattias
 
-----Original Message-----
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 another
level of deriative?

A virtual function override must have the same access
modifier that was used in declaring the virtual function
in the base class. This (and others) are most likely
protected because they are not meant to be directly
invoked by a user of the class but only via an event.
If MyForm class will be used as a parent class, how to handle the "static
void Main()" in its child classes?

You put the static Main() entry point in one and only one
class in your project. I believe it must be the first
class seen in any given source file too.
What's the default access specifier for "MyMethod()" if it doesn't have one?

In the above code it would be "private" because it is
declared within a class. The default access for something
at the top level is "public".

-- TB
 
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" <[email protected]>
From: "p988" <[email protected]>
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: <#[email protected]>
Newsgroups: microsoft.public.dotnet.languages.csharp
NNTP-Posting-Host: 237.los-angeles10rh16rt.ca.dial-access.att.net 12.80.67.237
Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!tk2msftngp13.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 another
level 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
 
You put the static Main() entry point in one and only one
class in your project. I believe it must be the first
class seen in any given source file too.

This statement is incorrect !

If your compilation includes more than one type with a Main method, you can
specify which type contains the Main method that you want to use as the
entry point into the program with compiler option /main:class


--
WBR, Roman S. Golubin
ICQ UIN 63253392
(e-mail address removed)

This posting is provided "AS IS" with no warranties, and confers no rights.
 
Back
Top