473,383 Members | 1,805 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

Where to put Constructor Code?

Does it make more sense to put additional constructor code WITHIN the
InitializeComponent(); method? Or should I put it right after the
InitializeComponent(); is called (in the public Form1() method)?
Nov 16 '05 #1
3 1461
Keith Smith wrote:
Does it make more sense to put additional constructor code WITHIN the
InitializeComponent(); method? Or should I put it right after the
InitializeComponent(); is called (in the public Form1() method)?


Well the InitializeComponent method is where the designer generated code
is placed. Although you can certainly add/change it if you want, if
there is no need to I would keep my own code out of there. You can place
your own constructor code before or after the call as long as you
realize that the designer generated code has to be executed before your
gui elements are initialized. So you would probably end up with
something like:

public Form1() {
//Ctor code that needs to be run before anything else
//InitializeComponent()
//Ctor code that needs to work with gui objects
}

---
Regards

Olle de Zwart
Nov 16 '05 #2

"Keith Smith" <ke*********@verizon.net> wrote in message
news:ZIJPd.19648$uc.17977@trnddc02...
Does it make more sense to put additional constructor code WITHIN the
InitializeComponent(); method? Or should I put it right after the
InitializeComponent(); is called (in the public Form1() method)?


public MyClass():System.Windows.Forms.Form.Form
{
public MyClass()
{
InitializeComponent();
initializeSomeMore();
}

// ...

initializeSomeMore();
{
// more initializing
}

private void MyClass_Load(object sender, System.EventArgs e)
{
// more initializing
}
}
Nov 16 '05 #3
Hi Keith,

I don't think you should put the code in InitializeComponent() method since
it is generated by desinger. Instead you could put your after/before the
InitializeComponent() call in ctor.

--
Regards,
Peter Jausovec
(http://blog.jausovec.net)
"Keith Smith" <ke*********@verizon.net> je napisal v sporoèilo
news:ZIJPd.19648$uc.17977@trnddc02 ...
Does it make more sense to put additional constructor code WITHIN the
InitializeComponent(); method? Or should I put it right after the
InitializeComponent(); is called (in the public Form1() method)?

Nov 16 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

23
by: Fabian Müller | last post by:
Hi all, my question is as follows: If have a class X and a class Y derived from X. Constructor of X is X(param1, param2) . Constructor of Y is Y(param1, ..., param4) .
19
by: Martin Oddman | last post by:
Hi, I have a compiling problem. Please take a look at the code below. I have an application that is built upon three tiers: one data tier (Foo.DataManager), one business tier (Foo.Kernel) and...
3
by: Magne Ryholt | last post by:
Assume a class DerivedClass has two constructors: DerivedClass(int a) { // some code } DerivedClass(int a, int b) : base(a, b) { }
9
by: Player | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hello all. I am in the process of teaching myself C# and I think I am doing OK. I have learnt how to how to call the right constructor of a...
4
by: Lasse Vågsæther Karlsen | last post by:
Ok, I realize that it doesn't really matter because it simply isn't possible, but does anyone know the reason why the constraint on the generic parameters for new() doesn't allow parameters? Ie....
10
by: Paul Cheetham | last post by:
Hi, I am developing an application that needs to store some machine-specific settings. The application is going to be published on the network in order to keep the clients on the latest version....
26
by: Patient Guy | last post by:
The code below shows the familiar way of restricting a function to be a method of a constructed object: function aConstructor(arg) { if (typeof(arg) == "undefined") return (null);...
7
by: Chris | last post by:
Hello all... I have a program with the following structure (all classes mentioned are of my own creation, and none of the classes contain try or catch blocks): - main() consists of a large...
10
by: active | last post by:
I have a couple of projects in the VS2005 format using Partial Class. I can't find where the code for sub New is stored. Can you tell me where it is? thanks
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.