Hi folks, hope you all had a good Christmas?
I'm wondering what is the correct way (as in where it should be done) to declare
and initialize my objects and variables in my classes? Should I declare and initialize all of them at the top of the class as in the following:
Or should I use private methods later on in my code to initialize my objects and variables as in the following:Code:namespace An_Example { public partial class Form1 : Form { //Declare and initialize here. //What would be considered a reasonable limit to the number //of objects and variables declared and initialized here? int myInteger = 10; Button myButton = new Button(); List<String> myList = new List<String>(); Object myObject = new Object(); Etc, etc. public Form1() { initializeComponent(); } } }
Thanks for your help in clearing this up for me.Code:namespace An_Example { public partial class Form1 : Form { //Declare, but don't initialize here //Should I use the keyword null? int myInteger = null; //Is this the correct way of doing things? int myInteger; //Or is this and the following examples the correct way? Button myButton; List<String> myList; Object myObject; Etc, etc. public Form1() { initializeComponent(); } //Use private methods here to initialize my objects and variables. } }




Reply With Quote