hey again guys and thanks for all your support in the past questions.
this is just a question of prefference really and what way is best.
i made a simple class as i was working through my study book, a class called Guy
i then came into the main form 1and then i made instances outside the form but inside the class so they can be accessed from any method in that form, the thing is though the initializers were split,
so rather than having my usual:
Guy john = New John();
i had it split in 2 parts like this:
public partial class Form1 : form
{
Guy john;
public Form1()
{
InitializeComponent();
john = new Guy ();
}
}
whay would they do this? the the book doesnt say so i tried to figure it out myself and came accross a discovery; the program will just fine i move the code about and join it back together like this:
public partial class Form1 : form
{
Guy john = new John();
public Form1()
{
InitializeComponent();
}
}
so the conculusion i came to is that it does not matter if the code is placed in 2 parts or as one line so that it can be accessed from the form in any method; it still does the same job. am i correct?
thanks
Loading
theLizardPosted Jan 4, 2011, 5:46 PM
public partial class Form1 : form
{
Guy john; // This declares the object as being global to the form
public Form1()
{
InitializeComponent();
john = new Guy (); //this instantiates the object and can be done anywhere on the form to be used anywhere on the form
}
}
doing this Guy john = New John(); before public Form1() is just the same as the above, so it seems that there is no difference where you do it, but in some cases it does matter where you do things.
In some cases you may need to initialize an objects value based on the results of other actions or return values.
so now we have,
public partial class Form1 : form
{
Guy john;
public Form1()
{
{
InitializeComponent();
john = new Guy(getLoginName()); //obviously this is an override of class Guy, there are many reasons why things like this are done, when and why will come with experience
}
}
Sometimes you cannot initialize an object until after the InitializeComponent(); because the value to initialize can only be obtained after it has been done.
this is true when an object needs to be instantiated with a value that depends on other components being initialized, in these cases you cannot do Guy john = new Guy(?,?,?) before the forms constructor.
hope this explains a bit better
John BurnsPosted Jan 5, 2011, 3:13 AM
Sam: i agree, i have been shocked ... but miracle's can happen :) its better if we all try and get along.
Sam HobbsPosted Jan 4, 2011, 7:22 PM
John BurnsPosted Jan 4, 2011, 9:58 AM
i prefer my findings on the 2nd example and keeping it all together and initializing the variable one the same line but just wasnt sure if there was an important reason behind why the book done it this way?
the only way i can think is that the reference variable would then be accesible to all methods using this form, but then again if i declare and instantate the class here as well it will still be accesible to all methods from inside the form. bit of a head scratcher ...
Sam HobbsPosted Jan 4, 2011, 9:43 AM
As for why do it one way or another, I think that in your example there is no difference. There are other situations in which we might have to use the second way. Is there any particular disadvantage to the first way? I think it is better to allocate (use new) an object in the same line that declares it.
John BurnsPosted Jan 4, 2011, 3:34 AM
but in the first aprroach i create the reference variable and then assign a value and initialize it shortly after wards, so when the program starts the object is still created but is just done in a separate way. the ref variable is set under public partial class Form1 : form then i assign the reference/ value under InitializeComponent();
am i getting lost here?
Mamta MPosted Jan 4, 2011, 3:25 AM
The 2 approaches are not the same. In the first approach, you are merely declaring a reference variable, you haven't actually created an object, therefore, it is not allocated any memory on the heap. It is only when you create an object that the heap memory is assigned to the object. In the second approach, you are declaring and instantiating in one go, therefore, memory is assigned right away.
This difference may seem subtle but makes a lot of difference when you have a fairly large number of such variables and also when you want to allocate memory only at a later stage, instead of right at the outset.
Hope this helps.