Recondaddy Posted September 19, 2012 Posted September 19, 2012 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. Quote
Administrators PlausiblyDamp Posted September 19, 2012 Administrators Posted September 19, 2012 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. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.