Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# inheritance and overriding base properties

I currently have a need for a custom ListViewItem class - let's call it MyListViewItem. It needs to have some additional data associated with each item, and perform some operations when the Checked property is changed. I've tried several things, but currently the relevant code looks like this:

class MyListViewItem : ListViewItem {
    new public bool Checked {
        get {
            return base.Checked;
        }
        set {
            base.Checked = value;
            // do some other things here based on value
        }
    }
    public MyListViewItem(Object otherData) {
        // ...
    }
}

The problem I'm having is that when I click on the item's checkbox in the ListView, my setter is never called. Does anyone know what I am doing wrong? I'm aware that I could use the ItemChecked event of the parent ListView, but that seems like a much less clean solution. (Also I'm not actually passing an Object to the constructor, but that part isn't important here).

like image 826
We Are All Monica Avatar asked May 13 '09 13:05

We Are All Monica


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is C full form?

Full form of C is “COMPILE”.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

What is C language basics?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.


1 Answers

It's not working cause the "new" keyword doesn't override it just "hides".

This means that if you call Checked on an instance of object that is referenced through the type definition of MyListViewItem you will run your code. However the ListView references to this object via the type definition of ListViewItem and therefore will not call your "new" method.

"new" is not override. The better solution is to probably handle the code in a custom list view. It isn't really that ugly.

like image 187
Quibblesome Avatar answered Sep 30 '22 01:09

Quibblesome