[C#] class inheritence and constructors...ew

Started by
5 comments, last by F-Kop 16 years, 6 months ago
Alrighty..I literally just switched from C to C# for GUI projects yesterday, and I have a question. I have a class called MainWindow that extends Gtk.Window. Gtk.Window has no constructor with 0 parameters. I'm trying to make a constructor with 0 parameters for MainWindow, but apparently it's trying to find Gtk.Window() which doesn't exist.

public class MainWindow : Gtk.Window
{
	public MainWindow( )
	{
		...
	}
	
	private void OnDelete( object o, Gtk.DeleteEventArgs e )
	{
		...
	}
}
So I'm assuming making a constructor automatically calls the parent class's constructor. What can I do about this? [Edited by - F-Kop on September 25, 2007 12:26:40 AM]
---------------------------------Upon thine ass shall I bust but a single cap.
Advertisement
me == C# n00b

FIXED
If Gtk.Window doesn't have a constructor with 0 parameters then your code shouldn't have any problems. If it does then you might run into trouble, in which case you should invoke the parent's constructor:

public class MainWindow : Gtk.Window {	public MainWindow() : base() { // Herein lies the magic. :)		...	}	private void OnDelete(object o, Gtk.DeleteEventArgs e) {		...	}}

[Website] [+++ Divide By Cucumber Error. Please Reinstall Universe And Reboot +++]

The error code is:

CS1501: No overload method for `Gtk.Window' takes `0' arguments.

And it points to this line:

public MainWindow( )
---------------------------------Upon thine ass shall I bust but a single cap.
building on what Benryves said, also I have never worked with GTK Framework.

public class MainWindow : Gtk.Window {	public MainWindow(<insert parameters needed for Gtk.Window Constructor here>) : base(<pass the parameters needed for Gtk.Window Constructor here from your constructor>)         { 		...	}	private void OnDelete(object o, Gtk.DeleteEventArgs e) {		...	}}
Its not a bug, its a feature!!
If you have some kind of constant value or known expression to pass to the constructor, you could do:

public MainWindow() : base(whateverYouNeed){}


(Edit) from googling, it looks like the Gtk.Window takes a string, so you could say:

public MainWindow() : base("Main Window!"){}
Ah thanks everyone for the replies. It works fine now. I was unsure of how to use the base thingy before. I guess that's what happens when you try to learn a new language in 20 minutes.
---------------------------------Upon thine ass shall I bust but a single cap.

This topic is closed to new replies.

Advertisement