Is this right?

R

R_O_O_K

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
 
J

Jon Skeet

R_O_O_K said:
If the static Main method belongs to some class should it have access to
the class' private members?

Yes, it should.
IMHO it shouldn't be so.

That would make life very tricky - how would Equals be implemented in
most cases, for example?
Does anyone know what the C# spec says?

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top