473,322 Members | 1,496 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,322 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 1217
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...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
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: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.