Jump to content
  • Advertisement
Sign in to follow this  
F-Kop

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

This topic is 4098 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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]

Share this post


Link to post
Share on other sites
Advertisement
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) {
...
}
}

Share this post


Link to post
Share on other sites
The error code is:

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

And it points to this line:

public MainWindow( )

Share this post


Link to post
Share on other sites
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) {
...
}
}




Share this post


Link to post
Share on other sites
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!")
{
}

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!