C# Constructors

D

David Morris

I am very new to C# (a couple of hours) but have worked with Java for
quite a while. One thing that isn't obvious and I can't find in the
documentation is how to call the superclass/based on class constructor
in a subclass. I want to create a base button class that overrides some
properties. Here is what I have:

using System;

namespace zone
{
/// <summary>
/// Summary description for ZoneBaseButton.
/// </summary>
public class ZoneBaseButton : System.Windows.Forms.Button
{
private void InitializeComponent() {
//
// ZoneBaseButton
//
this.FlatStyle = System.Windows.Forms.FlatStyle.System;
this.Font = new System.Drawing.Font("Microsoft Sans Serif",
20.25F, System.Drawing.FontStyle.Regular,
System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));

}

public ZoneBaseButton()
{
//
// TODO: Add constructor logic here
//
}
}
}

I saved this in a class library and added it to the toolbox. When I use
it the base class properties are not used. Does this type of code belong
in the InitializeComponent method or would it go in the Constructor? Is
this even a reasonable approach?

Thank you,

David Morris
 
D

Daniel O'Connell [C# MVP]

David Morris said:
I am very new to C# (a couple of hours) but have worked with Java for quite
a while. One thing that isn't obvious and I can't find in the documentation
is how to call the superclass/based on class constructor in a subclass. I
want to create a base button class that overrides some properties. Here is
what I have:

using System;

namespace zone
{
/// <summary>
/// Summary description for ZoneBaseButton.
/// </summary>
public class ZoneBaseButton : System.Windows.Forms.Button
{
private void InitializeComponent() {
//
// ZoneBaseButton
//
this.FlatStyle = System.Windows.Forms.FlatStyle.System;
this.Font = new System.Drawing.Font("Microsoft Sans Serif",
20.25F, System.Drawing.FontStyle.Regular,
System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));

}

public ZoneBaseButton()
{
//
// TODO: Add constructor logic here
//
}
}
}

I saved this in a class library and added it to the toolbox. When I use it
the base class properties are not used. Does this type of code belong in
the InitializeComponent method or would it go in the Constructor? Is this
even a reasonable approach?

Well, for one, you should be calling InitalizeComponent in your constructor.
You don't have to use the InitalizeComponent method as it is something
generated by the compiler and is not virtual.

I think, however, that your problem may stem from the difference in
virtualtiy in java and C#. In C#, everything is sealed(final in java, I
think) by default. You have to apply the virtual keyword to mark a method as
virtual and to override it you use override.

This approach should work, however.
 
D

David Morris

Thanks, it seems to be working now. I hate to ask such basic questions
but until you don't know the lingo it is difficult find the answer in
the manual.
 
D

Daniel O'Connell [C# MVP]

David Morris said:
Thanks, it seems to be working now. I hate to ask such basic questions but
until you don't know the lingo it is difficult find the answer in the
manual.
Ya, it can be. I came to C# from C(not C++) and some vb6...it was quite an
adjustment for me as well. Anyway, I forgot to answer your original
question. If you want to call a base constructor you use the base keyword as
below:

public class MyClass
{
public MyClass(string param) : base(param)
{

}
}
you use the same syntax to call another constructor in the current class,
just use this instead of base.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top