PC Review


Reply
Thread Tools Rate Thread

Constructors in C#

 
 
=?Utf-8?B?R3JlZyBIb3J3b29k?=
Guest
Posts: n/a
 
      3rd Jan 2005
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




 
Reply With Quote
 
 
 
 
Sahil Malik
Guest
Posts: n/a
 
      3rd Jan 2005
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" <(E-Mail Removed)> wrote in message
news:02CEAB2B-3C0A-4F9F-B245-(E-Mail Removed)...
> 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
>
>
>
>



 
Reply With Quote
 
=?Utf-8?B?R3JlZyBIb3J3b29k?=
Guest
Posts: n/a
 
      3rd Jan 2005
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" <(E-Mail Removed)> wrote in message
> news:02CEAB2B-3C0A-4F9F-B245-(E-Mail Removed)...
> > 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
> >
> >
> >
> >

>
>
>

 
Reply With Quote
 
Sahil Malik
Guest
Posts: n/a
 
      3rd Jan 2005
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" <(E-Mail Removed)> wrote in message
news:42F4AAC2-9423-41FB-8593-(E-Mail Removed)...
> 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" <(E-Mail Removed)> wrote in message
>> news:02CEAB2B-3C0A-4F9F-B245-(E-Mail Removed)...
>> > 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
>> >
>> >
>> >
>> >

>>
>>
>>



 
Reply With Quote
 
=?Utf-8?B?R3JlZyBIb3J3b29k?=
Guest
Posts: n/a
 
      3rd Jan 2005
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" <(E-Mail Removed)> wrote in message
> news:42F4AAC2-9423-41FB-8593-(E-Mail Removed)...
> > 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" <(E-Mail Removed)> wrote in message
> >> news:02CEAB2B-3C0A-4F9F-B245-(E-Mail Removed)...
> >> > 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
> >> >
> >> >
> >> >
> >> >
> >>
> >>
> >>

>
>
>

 
Reply With Quote
 
Tim Jarvis
Guest
Posts: n/a
 
      4th Jan 2005
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.
 
Reply With Quote
 
Sahil Malik
Guest
Posts: n/a
 
      4th Jan 2005
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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.



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Constructors? Arpan Microsoft ASP .NET 1 5th Sep 2006 07:33 AM
Constructors Peter Morris [Droopy eyes software] Microsoft C# .NET 68 29th Jul 2006 09:14 AM
Re: Constructors Scott Microsoft C# .NET 2 6th Aug 2003 06:51 AM
Re: Constructors Markus Egger Microsoft C# .NET 1 4th Aug 2003 11:10 PM
Constructors Ravikanth[MVP] Microsoft C# .NET 0 4th Aug 2003 10:48 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 12:50 PM.