472,958 Members | 2,556 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

should this be allowed?

look at the sampl

<pre

using System

class Bas
public virtual void Print(

Console.WriteLine( "Executing the base." )


class Derived : Bas
private new void Print(

Console.WriteLine( "Executing the derived." )
base.Print()
public void Print2(

Print()


class MainAp
static void Main(
Derived obj = new Derived()
obj.Print()

Console.WriteLine( "" )

obj.Print2()

</pre

now I basically have 2 versions of Print defined for Derived. one for external, one for internal. should the compiler actually allow this in the first place?
Nov 16 '05 #1
4 1209
Daniel Jin <an*******@discussions.microsoft.com> wrote:
look at the sample
<snip>
now I basically have 2 versions of Print defined for Derived. one for
external, one for internal. should the compiler actually allow this
in the first place?


Absolutely. The point of "new" is that it's defining a completely
different method. Whether or not it's a good idea to use this fact
arbitrarily is a different matter.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #2
The "new" keyword tells the compiler that you are defining a "new" method.
So, this makes perfect sense.
The compiler won't allow the restriction from public to private if you
replace "new" by "override".

Bruno.

"Daniel Jin" <an*******@discussions.microsoft.com> a écrit dans le message
de news:8D**********************************@microsof t.com...
look at the sample

<pre>

using System;

class Base
{

public virtual void Print()
{
Console.WriteLine( "Executing the base." );
}

}

class Derived : Base
{

private new void Print()
{
Console.WriteLine( "Executing the derived." );
base.Print();
}

public void Print2()
{
Print();
}

}

class MainApp
{

static void Main()
{

Derived obj = new Derived();
obj.Print();

Console.WriteLine( "" );

obj.Print2();

}

}

</pre>

now I basically have 2 versions of Print defined for Derived. one for

external, one for internal. should the compiler actually allow this in the
first place?
Nov 16 '05 #3


----- Jon Skeet [C# MVP] wrote: ----
Absolutely. The point of "new" is that it's defining a completely
different method. Whether or not it's a good idea to use this fact
arbitrarily is a different matter


Just seemed a little odd to me since Print from base class isn't hidden because the difference in visibility. Wouldn't it be better if the compiler at least generated a warning? say hypothetically, I wanted to make the new Print() public in Derived, but for some reason I forgot to mark it with public, so it defaulted to private. compiler doesn't complain. now I call Derived.Print(), but the base method executes. It'd be probably very easy to spot a mistake like this, but still, warning would be nice.
Nov 16 '05 #4
"new" methods are a rather dangerous feature IMO. So, you have to be careful
when you use it. Given its semantics, it is difficult for the compiler to
give you this kind of warning.

I try to restrict the use of new to cases where the new method performs
exactly the same thing as the method in the superclass but has a different
return type. The typical pattern is the following:

class BaseContainer { }
class BaseElement
{
private BaseContainer _container;
public BaseContainer Container { get { return _container; } }
}

class DerivedContainer : BaseContainer {}
class DerivedElement : BaseElement
{
public new DerivedContainer Container { get { return
(DerivedContainer)base.Container; } }
}

The idea is to get a correctly typed API in the derived API. (this is called
type covariance and languages like Eiffel support it).

But I don't allow any other use of new methods, because anything else
(redefining a method to give it different semantics than what it has in the
base class) is just heading for trouble. If the two methods do different
things, you get different results depending on whether you call them from a
BaseXxx or a DerivedXxx (the types that the compiler sees, not the runtime
types), and this can lead to very tricky bugs.

Bruno.

"Daniel Jin" <an*******@discussions.microsoft.com> a écrit dans le message
de news:7D**********************************@microsof t.com...


----- Jon Skeet [C# MVP] wrote: -----
> Absolutely. The point of "new" is that it's defining a completely
> different method. Whether or not it's a good idea to use this fact
> arbitrarily is a different matter.


Just seemed a little odd to me since Print from base class isn't

hidden because the difference in visibility. Wouldn't it be better if the
compiler at least generated a warning? say hypothetically, I wanted to make
the new Print() public in Derived, but for some reason I forgot to mark it
with public, so it defaulted to private. compiler doesn't complain. now I
call Derived.Print(), but the base method executes. It'd be probably very
easy to spot a mistake like this, but still, warning would be nice.
Nov 16 '05 #5

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

Similar topics

4
by: Larry | last post by:
I believe the .Net XmlValidatingReader should fail when validating XML that contains a ComplexType element with white space when the ComplexType element has the mixed attribute set to false in the...
10
by: Philip Sherman | last post by:
SELECT columns FROM t1 JOIN t2
10
by: Lighter | last post by:
#include <set> using namespace std; int main() { typedef set<intINTSET; INTSET coll;
7
by: Bernard Dhooghe | last post by:
Environment: DB2 UDB LUW 8.2 Following problem was submitted. create table t1 (c1 char(10) not null) insert into t1 values(default) This will result in (db2 command line):
2
by: Sound | last post by:
Enter space bar for field names and save the field.The field shoud not get saved and an alert should be there as"Space bars are not allowed" Add a field without giving any name and save the...
82
by: quiberon2 | last post by:
Hi, Sorry if it might be a stupid question but what should returns malloc(0) ? void *ptr = malloc(0); I am running gcc 3.3.5 and a non-null address is returned. ( in the compiler that I am...
2
by: Bill Fallon | last post by:
I have a VS2005 VB.Net windows form application deployed to a share drive. The windows explorer security permissions for this application (.exe) file is set for Everyone with List Folder/Read Data...
9
by: Abandoned | last post by:
Hi.. I want to delete all now allowed characters in my text. I use this function: def clear(s1=""): if s1: allowed = s1 = "".join(ch for ch in s1 if ch in allowed) return s1
4
by: jmarcrum | last post by:
Hi everyone!! I have a continuous form that allows users to select a record and chnage the DATE that the record is assigned to another DATE within the same year. The button is called "Change plan...
2
by: puzzlecracker | last post by:
Unlike C++, in Csharp you're only allowed to compare a generic type T with null, if the method it's passed in not implementing IComparable<Tor , IEquatable<T(still don't know why we need these...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
2
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.