Is this right?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • R_O_O_K

    Is this right?

    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();
    PrintPreviewCon trol ppv = new PrintPreviewCon trol();
    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 PrintPageEventH andler(cls.OnPr intPage);



    Application.Run (frm);
    }
    private void OnPrintPage(obj ect obj, PrintPageEventA rgs ppea)
    {
    }
    }


    Regards!
    Micha³ Ziemski

  • Jon Skeet

    #2
    Re: Is this right?

    R_O_O_K <rook@roo.k.p l> wrote:[color=blue]
    > If the static Main method belongs to some class should it have access to
    > the class' private members?[/color]

    Yes, it should.
    [color=blue]
    > IMHO it shouldn't be so.[/color]

    That would make life very tricky - how would Equals be implemented in
    most cases, for example?
    [color=blue]
    > Does anyone know what the C# spec says?[/color]

    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.co m>
    Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

    If replying to the group, please do not mail me too

    Comment

    Working...