Groups | Blog | Home
all groups > dotnet general > july 2003 >

dotnet general : Is this right?


R_O_O_K
7/11/2003 11:18:41 PM
Hi!

If the static Main method belongs to some class should it have access to
the class' private members? IMHO it shouldn't be so. Does anyone know
what the C# spec says? Here is a code example that compiles fine on
VC# 2002.

class Class1
{
[STAThread]
static void Main(string[] args)
{
Class1 cls = new Class1();
Form frm = new Form();
PrintPreviewControl ppv = new PrintPreviewControl();
ppv.Parent = frm;
ppv.Dock = DockStyle.Fill;
frm.Show();

PrintDocument doc = new PrintDocument();
ppv.Document = doc;

// here's the call to cls' private member
doc.PrintPage += new PrintPageEventHandler(cls.OnPrintPage);



Application.Run(frm);
}
private void OnPrintPage(object obj, PrintPageEventArgs ppea)
{
}
}


Regards!
Micha³ Ziemski
Jon Skeet
7/11/2003 11:55:15 PM
[quoted text, click to view]

Yes, it should.

[quoted text, click to view]

That would make life very tricky - how would Equals be implemented in
most cases, for example?

[quoted text, click to view]

From 10.5.2:

<quote>
Private, which is selected by including a private modifier in the
member declaration. The intuitive meaning of private is "access limited
to the containing type".
</quote>

Here, Main is in the containing type, so has access to the member.

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet/
AddThis Social Bookmark Button