473,385 Members | 1,453 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Using 'new' to override non-virtual methods

I am writing a class that inherits from TextBox. My class,
UndoTextBox, extends the standard control by enabling multi-level undo
and redo operations.

Annoyingly, some of the affected properties and methods (such as
CanUndo and Clear) are not virtual and therefore cannot be overridden.
I have hacked around this by declaring 'new' methods, e.g.

public new bool CanUndo
{
get { return m_undoStack.CanUndo; }
}

Since "a constant, field, property, or type introduced in a class or
struct hides all base class members with the same name", I assume that
the correct CanUndo will be called even if, say, the object in
question has been cast to TextBoxBase.

However, this still feels like a (necessary) hack. Are there any other
repercussions I should take into account when using 'new' like this?

P.
Nov 16 '05 #1
1 2129
Inline

--
C#, .NET and Complex Adaptive Systems:
http://blogs.geekdojo.net/Richard
"Paul E Collins" <fi******************@CL4.org> wrote in message
news:cc**********@sparta.btinternet.com...
I am writing a class that inherits from TextBox. My class,
UndoTextBox, extends the standard control by enabling multi-level undo
and redo operations.

Annoyingly, some of the affected properties and methods (such as
CanUndo and Clear) are not virtual and therefore cannot be overridden.
I have hacked around this by declaring 'new' methods, e.g.

public new bool CanUndo
{
get { return m_undoStack.CanUndo; }
}

Since "a constant, field, property, or type introduced in a class or
struct hides all base class members with the same name", I assume that
the correct CanUndo will be called even if, say, the object in
question has been cast to TextBoxBase.
No, if by the "correct" CanUndo you mean your "new" CanUndo - it won't be
called if the reference is cast to TextBoxBase. That is the difference
between overriding and hiding via 'new'. Your CanUndo member is truly
"hidden" from TextBoxBase - the base class can never access it (unless you
were able to go back and make TextBoxBase aware of its derived type - not
usually a good idea!). How you get around this limitation really depends
on the context of the situation, but I'm afraid there's no way to directly
override non-virtual members.

However, this still feels like a (necessary) hack. Are there any other
repercussions I should take into account when using 'new' like this?

P.

Nov 16 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: grutta | last post by:
I am writing a windows service that will recieve notification when a USB Device is insterted into the machine. I have used the RegisterDeviceNotification and the RegisterServiceCtrlHandlerEx with...
4
by: gsyoon | last post by:
hi, all. I'm trying to make a "framework" to store the return value of a function to a global memory. My first attempt was 1) void store_to_global( char * type_name ) { if ( strcmp(...
1
by: Jason Hickey | last post by:
Has there been a change in the way the UI designer handles winform inheritance in the 2003 version of visual studio. Consider the following (try it if you are using 2003 Everything seems to work...
3
by: TT (Tom Tempelaere) | last post by:
Hi there I am making a service project in C#, and I'm in the process of writing the installer. I made an installer class by using the "Add Installer" menu-item in the design window of the service,...
0
by: Marco Segurini | last post by:
Hi, I am trying to dynamically install/deinstall a message handler to a System.Windows.Forms.Form using NativeWindow. I do not use IMessageFilter derived class because it intercept only the...
4
by: Dante | last post by:
Hello, When I try to decompress a response from a web service I'm getting the error: "hexadecimal value 0x1F, is an invalid character. Line 1, position 1." The web server is an apache server....
4
by: Kevin Phifer | last post by:
Ok, before anyone freaks out, I have a solution I need to create that gathers content from maybe different places. Each one can return a <form> in the html, so its the classic can't have more than...
3
by: msnews.microsoft.com | last post by:
Hello All, I am trying to write Web Controls and in most of the samples I came across, I am seeing the following function where a HTML string is written to create HTML Controls. ///...
30
by: Pep | last post by:
Is it best to include the code "using namespace std;" in the source or should each keyword in the std namespace be qualified by the namespace tag, such as std::cout << "using std namespace" <<...
6
by: Tony | last post by:
Hello! Below I have a complete working program.with some simple classes one of these is a generic class. Now If I want to implement functionallity so I can compare animal with each other or...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.