Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does this derived class behave different from it's base class

In some obscure way a derived class which doesn't add new functionality (yet) behaves different from it's base class. The derived class:

public class MyCheckButton : CheckButton
{
    public MyCheckButton(string label) : base(label)
    {
    }
}

MyCheckButton inherits from a (GTK#, part of the Mono project) CheckButton. However in the following code snippet they behave differently:

var button1 = new CheckButton("_foo");
var button2 = new MyCheckButton("_foo");
// code omitted

The underscore in the label makes sure that the label gets a mnemonic. For button1 this works in my testcode: I get "foo" where the f is underlined. However for button2 this fails. I just get "_foo" as a label in my dialog.

Can anyone explain how the derived class in this example could behave differently or is there some magic going on behind the screen that maybe checks the type of the actual class?

like image 247
Maurits Rijk Avatar asked Jul 12 '11 19:07

Maurits Rijk


1 Answers

[I]s there some magic going on behind the screen that maybe checks the type of the actual class?

Actually, there is:

public CheckButton(string label) : base(IntPtr.Zero)
{
    if (base.GetType() != typeof(CheckButton))
    {
        ArrayList arrayList = new ArrayList();
        ArrayList arrayList2 = new ArrayList();
        arrayList2.Add("label");
        arrayList.Add(new Value(label));
        this.CreateNativeObject((string[])arrayList2.ToArray(typeof(string)), (Value[])arrayList.ToArray(typeof(Value)));
    }
    else
    {
        IntPtr intPtr = Marshaller.StringToPtrGStrdup(label);
        this.Raw = CheckButton.gtk_check_button_new_with_mnemonic(intPtr);
        Marshaller.Free(intPtr);
    }
}

It looks like your subclass will be going the former route. Not sure why that would mess up the mnemonic, though; the latter method is a P/Invoke on the native gtk library. It's possible that wrapping label in a Value object is mucking up the mnemonic stuff.

Let that be a lesson (to the GTK# designers): don't violate the Liskov Substitution Principle. It's confusing!

like image 129
dlev Avatar answered Oct 26 '22 02:10

dlev