Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# VB.NET
if we have a "constructor" and the "main()" method whichone will called first on the execution of the form/page?
Posted 19-Feb-13 23:48pm
Comments
Fred Flams at 20-Feb-13 5:54am
   
Well, there is no correct answer to that question I guess.
I you "Main" method is static and declared as your application entry point then it will be called first, otherwise only the constructor will be called
boogac at 20-Feb-13 6:03am
   
constructor can be static also..there will be answers sure but i think best way debug and see
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Main is a static entry point method for your application. When the application starts, Main is called first. After that it is not called anymore in normal circumstances. Note that Main, does not "construct" anything.! If you look at a normal C# winform application it will say something like this:
 
static class Program {
		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main() {
		  Application.EnableVisualStyles();
		  Application.SetCompatibleTextRenderingDefault(false);
 
                  //new Form1 CONSTRUCTS the form.
		  Application.Run(new Form1());
		}
	}
 
The Constructor is called when you generate an object from a class with the new keyword.
It is the method that "constructs" the "object" from a template = the "class". This is done by instantiating the variables, event, methods, ...
 
Hope this helps.
  Permalink  
Comments
CHill60 at 20-Feb-13 6:04am
   
My 5. Nice, concise and clear
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

No, it doesn't work like that! Laugh | :laugh:
 
There is (or should be) only one Main method per project / application, and it will be called once and only once when your application starts, before any objects are constructed. If will then construct the objects needed to run you application and that includes the form in a WinForms app. Normally, you don't need to even look at it, much less change it! Big Grin | :-D
 
Every time you create an instance of a class, the class constructor will be called immediately - as it is the code which actually builds the object instance and sets it starting values.
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 861
1 OriginalGriff 410
2 CPallini 255
3 Volynsky Alex 205
4 Richard Deeming 175
0 OriginalGriff 5,540
1 Sergey Alexandrovich Kryukov 4,668
2 CPallini 4,540
3 George Jonsson 3,102
4 Gihan Liyanage 2,445


Advertise | Privacy | Mobile
Web01 | 2.8.140916.1 | Last Updated 20 Feb 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100