|
-
Jan 16th, 2007, 12:47 PM
#1
Thread Starter
Member
[RESOLVED] Declaring and initialising objects and variables - where should it be done?
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:
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();
}
}
}
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, 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.
}
}
Thanks for your help in clearing this up for me.
Using: VB.net + C#.net 2005 Express Editions + .Net Framework 2.0
~ Man Is The Warmest Place To Hide ~
-
Jan 16th, 2007, 05:12 PM
#2
Re: Declaring and initialising objects and variables - where should it be done?
If there is a chance that a particular object may not be used then you may prefer to leave the variable uninitialised and only instantiate if and when the object is required. If an object may not be needed for some time after the parent object is created you may also like to defer instantiation until the object is actually required. Otherwise you should instantiate the object on the same line the variable is declared because the compiler is able to optimise that code.
As for initialising variables to null that have no other value, you should always do so for local variables but for member variables it is unnecessary. Member variables that are not explicitly initialised are assumed to be null while uninitialised local variables are exactly that. Try entering this code and see the error message for proof.
Code:
private object obj1;
private void Method1()
{
object obj2;
this.Method2(obj1, obj2);
}
private void Method2(object obj1, object obj2)
{
}
It will tell you that obj2 hasn't been initialised but there's no issue with obj1.
-
Jan 17th, 2007, 03:32 PM
#3
Thread Starter
Member
Re: Declaring and initialising objects and variables - where should it be done?
Hi jmcilhinney,
Prior to your post I was unsure as to how I should proceed with my coding, but what you've said has cleared things up for me. Thank you.
Regards,
The Thing.
Using: VB.net + C#.net 2005 Express Editions + .Net Framework 2.0
~ Man Is The Warmest Place To Hide ~
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|