Constructors in C#

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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
 
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/sahilmalik/archive/2004/10/07/27986.aspx
..Other than that, everything is hunky dory.

- Sahil Malik
http://dotnetjunkies.com/weblog/sahilmalik
 
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
 
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
 
Thanks heaps Sahil. I'll give it a go.

Sahil Malik said:
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 said:
Thanks heaps Sahil. I'll give it a go.

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.
 
LOL Tim, I like my doggie example .. I put it on my blog first to
demonstrate Generics power --->

http://dotnetjunkies.com/WebLog/sahilmalik/archive/2005/01/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
 
Back
Top