473,396 Members | 2,003 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,396 software developers and data experts.

"protected" access specifier

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?


Nov 15 '05 #1
4 7319
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

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/
Please reply only to the newsgroup.
Nov 15 '05 #2
-----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 anotherlevel 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 "staticvoid 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

Nov 15 '05 #3
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

Nov 15 '05 #4

<an*******@discussions.microsoft.com> wrote:
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.


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
go*****@arhcity.ru

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

Nov 15 '05 #5

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

Similar topics

28
by: Act | last post by:
Why is it suggested to not define data members as "protected"? Thanks for help!
2
by: Andreas Klemt | last post by:
Hello, what is the difference between a) Protected WithEvents myClassName b) Protected myClassName Thanks, Andreas
3
by: Jordan Taylor | last post by:
I am confused about protected member functions and objects. Is there any particular advantage of declaring members protected?
4
by: Tina | last post by:
This is an issue regarding what exactly is accomplished by using "Protected" when defining a variable. It seems it does much more than just applying Protected status to a variable. I have an...
2
by: Rob Richardson | last post by:
Greetings! Consider the following: class CBase { public: CBase(); virtual ~CBase();
7
by: =?Utf-8?B?cmFtbzk5NDE=?= | last post by:
Hi, Is there any way disable using "protected override void OnPaint(..." method out of the assembly. I create usercontrol and i do not want user adds extra codes OnPaint method for securtity...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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,...
0
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...

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.