Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Hello everyone,

 

I'm not a software engineer, so I'm certain this is probably something that you guys see all the time, but it's a first for me.

 

I'm reverse engineering a sample C# console application, and I ran across a scenario I don't quite understand -- a class instantiating itself within itself. Here's a simplified example of what I mean:

 

class Program
   {
       static void Subroutine1()
       {
          DoSomething
       }

       static void Main(string[] args)
       {
           Subroutine1();

           [b]Program program = new Program();[/b]
           ...
           ...
           ...
           program.Subroutine2();
       }

       void Subroutine2()
       {
            DoSomethingElse
       }
    }

 

I hope that's enough to see what I'm talking about. Does anyone know what the developer was trying to accomplish here?

 

Thanks for any help you can provide.

  • Administrators
Posted
In that example the class is being instantiated from the Main method, this method is declared as static and cannot access any instance members only static ones such as Subroutine1 , by creating an instance of the Program class the Main method can access instance members such as Subroutine2 through the program instance.

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...