473,516 Members | 2,956 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Constructors in C#

Dear All,

I wish to populate various fields in an additional windows form which is
triggered by the main app.

Passing the information from the main app to the dialog is proving to be
difficult. Obviously I don't want to alter the controls' modifiers and thus I
would normally create an additional constructor (like in VC++) to take care
of accepting the variables.

In other words:
Form2()
Form2(int param1, string param2)

My question is: Is this approach the normal way to go? And if so, how does
one create an alternate contructor? Or is that something which belongs to C++.

Any help really appreciated.

Thanks

Greg


Nov 16 '05 #1
6 1411
Greg,

You can very easily create overloaded constructors in C#.

So if you had a class called Dog

public class Dog
{
private string strDoggiename = "" ;
public Dog() // Default constructor
{
}

public Dog(string doggiename)
{
strDoggiename = doggiename;
}
}

The above is a valid class that you can use as either of below.

Dog ruffie = new Dog() ;

or

Dog ruffie = new Dog("ruffie") ;

The only restriction .NET imposes compared with C++ is that, if your base
class did not implement a default constructor, the inherited classes must
explicitly call a non default constructor in their constructors. That is
explained over here -
http://dotnetjunkies.com/WebLog/sahi.../07/27986.aspx
..Other than that, everything is hunky dory.

- Sahil Malik
http://dotnetjunkies.com/weblog/sahilmalik

"Greg Horwood" <Gr*********@discussions.microsoft.com> wrote in message
news:02**********************************@microsof t.com...
Dear All,

I wish to populate various fields in an additional windows form which is
triggered by the main app.

Passing the information from the main app to the dialog is proving to be
difficult. Obviously I don't want to alter the controls' modifiers and
thus I
would normally create an additional constructor (like in VC++) to take
care
of accepting the variables.

In other words:
Form2()
Form2(int param1, string param2)

My question is: Is this approach the normal way to go? And if so, how does
one create an alternate contructor? Or is that something which belongs to
C++.

Any help really appreciated.

Thanks

Greg

Nov 16 '05 #2
Thanks for you reply Sahil. However, I'm still a little confused as to how to
permorm this using the Visual Studio IDE. I'm glad to hear that the same
constructor mentality exists in C#, it's just the practicalities I'm
struggling with.
For instance, using you Dog class, where in the output code developed by
Visual Studio would you create this constructor? After Form1()? Or where the
TODO tells you to put constructor information.
Thanks

Greg

"Sahil Malik" wrote:
Greg,

You can very easily create overloaded constructors in C#.

So if you had a class called Dog

public class Dog
{
private string strDoggiename = "" ;
public Dog() // Default constructor
{
}

public Dog(string doggiename)
{
strDoggiename = doggiename;
}
}

The above is a valid class that you can use as either of below.

Dog ruffie = new Dog() ;

or

Dog ruffie = new Dog("ruffie") ;

The only restriction .NET imposes compared with C++ is that, if your base
class did not implement a default constructor, the inherited classes must
explicitly call a non default constructor in their constructors. That is
explained over here -
http://dotnetjunkies.com/WebLog/sahi.../07/27986.aspx
..Other than that, everything is hunky dory.

- Sahil Malik
http://dotnetjunkies.com/weblog/sahilmalik

"Greg Horwood" <Gr*********@discussions.microsoft.com> wrote in message
news:02**********************************@microsof t.com...
Dear All,

I wish to populate various fields in an additional windows form which is
triggered by the main app.

Passing the information from the main app to the dialog is proving to be
difficult. Obviously I don't want to alter the controls' modifiers and
thus I
would normally create an additional constructor (like in VC++) to take
care
of accepting the variables.

In other words:
Form2()
Form2(int param1, string param2)

My question is: Is this approach the normal way to go? And if so, how does
one create an alternate contructor? Or is that something which belongs to
C++.

Any help really appreciated.

Thanks

Greg


Nov 16 '05 #3
Greg,

A form is nothing but a class. I don't have VS2003 IDE in front of me, so
I'll try peice this from my memory, but my understanding is that in VS2003,
when you add a form, it creates a default constructor for you, that looks
somewhat like

class Form1 : System.Windows.Forms.Form
{
.... some other code .. who cares ..
... some more code who cares ..
public Form1()
{
((Was there a call to InitializeComponent here? I don't remember)).
// TODO: Write your custom logic here.
}

public Form1(string doggieName) // This is where the nondefault
constructor would go.
{
}
.... some other code .. who cares ..
... some more code who cares ..
}
.... I might add, the EXACT placement is irrelevant, as long as it is within

public class Form1
{
...
}

and as long as it looks like another method at the same level as the default
constructor.

... If it is still unclear, attach your Form1.cs with your reply and I'd be
happy to modify it.

- Sahil Malik
http://dotnetjunkies.com/weblog/sahilmalik


"Greg Horwood" <Gr*********@discussions.microsoft.com> wrote in message
news:42**********************************@microsof t.com...
Thanks for you reply Sahil. However, I'm still a little confused as to how
to
permorm this using the Visual Studio IDE. I'm glad to hear that the same
constructor mentality exists in C#, it's just the practicalities I'm
struggling with.
For instance, using you Dog class, where in the output code developed by
Visual Studio would you create this constructor? After Form1()? Or where
the
TODO tells you to put constructor information.
Thanks

Greg

"Sahil Malik" wrote:
Greg,

You can very easily create overloaded constructors in C#.

So if you had a class called Dog

public class Dog
{
private string strDoggiename = "" ;
public Dog() // Default constructor
{
}

public Dog(string doggiename)
{
strDoggiename = doggiename;
}
}

The above is a valid class that you can use as either of below.

Dog ruffie = new Dog() ;

or

Dog ruffie = new Dog("ruffie") ;

The only restriction .NET imposes compared with C++ is that, if your base
class did not implement a default constructor, the inherited classes must
explicitly call a non default constructor in their constructors. That is
explained over here -
http://dotnetjunkies.com/WebLog/sahi.../07/27986.aspx
..Other than that, everything is hunky dory.

- Sahil Malik
http://dotnetjunkies.com/weblog/sahilmalik

"Greg Horwood" <Gr*********@discussions.microsoft.com> wrote in message
news:02**********************************@microsof t.com...
> Dear All,
>
> I wish to populate various fields in an additional windows form which
> is
> triggered by the main app.
>
> Passing the information from the main app to the dialog is proving to
> be
> difficult. Obviously I don't want to alter the controls' modifiers and
> thus I
> would normally create an additional constructor (like in VC++) to take
> care
> of accepting the variables.
>
> In other words:
> Form2()
> Form2(int param1, string param2)
>
> My question is: Is this approach the normal way to go? And if so, how
> does
> one create an alternate contructor? Or is that something which belongs
> to
> C++.
>
> Any help really appreciated.
>
> Thanks
>
> Greg
>
>
>
>


Nov 16 '05 #4
Thanks heaps Sahil. I'll give it a go.

"Sahil Malik" wrote:
Greg,

A form is nothing but a class. I don't have VS2003 IDE in front of me, so
I'll try peice this from my memory, but my understanding is that in VS2003,
when you add a form, it creates a default constructor for you, that looks
somewhat like

class Form1 : System.Windows.Forms.Form
{
.... some other code .. who cares ..
... some more code who cares ..
public Form1()
{
((Was there a call to InitializeComponent here? I don't remember)).
// TODO: Write your custom logic here.
}

public Form1(string doggieName) // This is where the nondefault
constructor would go.
{
}
.... some other code .. who cares ..
... some more code who cares ..
}
.... I might add, the EXACT placement is irrelevant, as long as it is within

public class Form1
{
...
}

and as long as it looks like another method at the same level as the default
constructor.

... If it is still unclear, attach your Form1.cs with your reply and I'd be
happy to modify it.

- Sahil Malik
http://dotnetjunkies.com/weblog/sahilmalik


"Greg Horwood" <Gr*********@discussions.microsoft.com> wrote in message
news:42**********************************@microsof t.com...
Thanks for you reply Sahil. However, I'm still a little confused as to how
to
permorm this using the Visual Studio IDE. I'm glad to hear that the same
constructor mentality exists in C#, it's just the practicalities I'm
struggling with.
For instance, using you Dog class, where in the output code developed by
Visual Studio would you create this constructor? After Form1()? Or where
the
TODO tells you to put constructor information.
Thanks

Greg

"Sahil Malik" wrote:
Greg,

You can very easily create overloaded constructors in C#.

So if you had a class called Dog

public class Dog
{
private string strDoggiename = "" ;
public Dog() // Default constructor
{
}

public Dog(string doggiename)
{
strDoggiename = doggiename;
}
}

The above is a valid class that you can use as either of below.

Dog ruffie = new Dog() ;

or

Dog ruffie = new Dog("ruffie") ;

The only restriction .NET imposes compared with C++ is that, if your base
class did not implement a default constructor, the inherited classes must
explicitly call a non default constructor in their constructors. That is
explained over here -
http://dotnetjunkies.com/WebLog/sahi.../07/27986.aspx
..Other than that, everything is hunky dory.

- Sahil Malik
http://dotnetjunkies.com/weblog/sahilmalik

"Greg Horwood" <Gr*********@discussions.microsoft.com> wrote in message
news:02**********************************@microsof t.com...
> Dear All,
>
> I wish to populate various fields in an additional windows form which
> is
> triggered by the main app.
>
> Passing the information from the main app to the dialog is proving to
> be
> difficult. Obviously I don't want to alter the controls' modifiers and
> thus I
> would normally create an additional constructor (like in VC++) to take
> care
> of accepting the variables.
>
> In other words:
> Form2()
> Form2(int param1, string param2)
>
> My question is: Is this approach the normal way to go? And if so, how
> does
> one create an alternate contructor? Or is that something which belongs
> to
> C++.
>
> Any help really appreciated.
>
> Thanks
>
> Greg
>
>
>
>


Nov 16 '05 #5
Greg Horwood wrote:
Thanks heaps Sahil. I'll give it a go.

"Sahil Malik" wrote:
Greg,


Hi Greg,

Just to add to Sahils reply, you can also chain your constructors
together (including base constructors) so that you don't need to
duplicate code that is in the other constructors.

i.e.
public Form1()
{
InitializeComponents();
}

public Form1(string doggieName) : this()
{
// this saves you having to call initializeComponents in here as well.
}

public Form1(string doggieName, bool doggieWalked) : this(doggieName)
{
// ok now chained to both previous constructors
}
Regards Tim.
Nov 16 '05 #6
LOL Tim, I like my doggie example .. I put it on my blog first to
demonstrate Generics power --->

http://dotnetjunkies.com/WebLog/sahi.../02/40506.aspx

BTW, there's another ultra neat trick you can try with chained constructors.

Say you're implementing an exception, and you want the exception message to
be decided based upon an integer that you pass in.

You can do MyExceptionConstructor(int i) : base("Some Message")

but you can't do ..

MyExceptionConstructor(int i) : base()
{
this.Message = "Something that changes based upon the value of i" ; //
because exception.message is read-only
}

So whaddya do??

MyExceptionConstructor(int i) : base (mystaticmethod(i))
{
}

where the logic sits in mystaticmethod .. and that returns a string

Kinda neat huh? :)

- Sahil Malik
http://dotnetjunkies.com/weblog/sahilmalik

"Tim Jarvis" <Ti*******@newsgroup.nospam> wrote in message
news:eq**************@TK2MSFTNGP14.phx.gbl...
Greg Horwood wrote:
Thanks heaps Sahil. I'll give it a go.

"Sahil Malik" wrote:
> Greg,


Hi Greg,

Just to add to Sahils reply, you can also chain your constructors
together (including base constructors) so that you don't need to
duplicate code that is in the other constructors.

i.e.
public Form1()
{
InitializeComponents();
}

public Form1(string doggieName) : this()
{
// this saves you having to call initializeComponents in here as well.
}

public Form1(string doggieName, bool doggieWalked) : this(doggieName)
{
// ok now chained to both previous constructors
}
Regards Tim.

Nov 16 '05 #7

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

Similar topics

42
5721
by: Edward Diener | last post by:
Coming from the C++ world I can not understand the reason why copy constructors are not used in the .NET framework. A copy constructor creates an object from a copy of another object of the same kind. It sounds simple but evidently .NET has difficulty with this concept for some reason. I do understand that .NET objects are created on the GC...
3
1629
by: John | last post by:
Before anything else, thanks Marina, Workgroups and Ralf, for your help so far. I am now able to better define the question! After adding more console printout lines to CSum, I tried all permutations for constructors (none, default, two argument) and method call in body of constructor (none and one). Maybe this example is not...
42
1810
by: Edward Diener | last post by:
Coming from the C++ world I can not understand the reason why copy constructors are not used in the .NET framework. A copy constructor creates an object from a copy of another object of the same kind. It sounds simple but evidently .NET has difficulty with this concept for some reason. I do understand that .NET objects are created on the GC...
0
7276
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
1
7142
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
5714
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5110
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4773
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3259
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1624
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
825
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
488
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.