Click here to Skip to main content
15,894,316 members
Home / Discussions / C#
   

C#

 
GeneralRe: inheriting varibles from within a class Pin
Luc Pattyn12-May-09 17:22
sitebuilderLuc Pattyn12-May-09 17:22 
GeneralRe: inheriting varibles from within a class Pin
PIEBALDconsult12-May-09 17:41
mvePIEBALDconsult12-May-09 17:41 
GeneralRe: inheriting varibles from within a class Pin
antrock10112-May-09 17:25
antrock10112-May-09 17:25 
GeneralRe: inheriting varibles from within a class Pin
antrock10112-May-09 17:36
antrock10112-May-09 17:36 
GeneralRe: inheriting varibles from within a class Pin
PIEBALDconsult12-May-09 17:46
mvePIEBALDconsult12-May-09 17:46 
GeneralRe: inheriting varibles from within a class Pin
antrock10112-May-09 18:00
antrock10112-May-09 18:00 
GeneralUsing the New keyword Pin
Mycroft Holmes12-May-09 15:23
professionalMycroft Holmes12-May-09 15:23 
GeneralRe: Using the New keyword Pin
Luc Pattyn12-May-09 17:09
sitebuilderLuc Pattyn12-May-09 17:09 
Hi Mycroft,

You are right to aim for a build without warning messages; so when a warning about hiding, "new" or "override" appears, you should act on it. However you can't just put "new" everywhere.

This is how I understand the situation, assuming a base class and a derived class:
- when a method is not virtual in the base class, it can not be overridden; if it appears in the derived class it needs "new"
- when a method is virtual in the base class, it needs either "override" or "new" and the result is different, see the example.
- when a method is absent in the base class, it should not have "override" nor "new" in a derived class.

public void Test() {
	A a=new A();
	a.print("A=new A");
	B b=new B();
	b.print("B=new B");
	C c=new C();
	c.print("C=new C");
	A ab=new B();
	ab.print("A=new B");
	A ac=new A();
	ac.print("A=new C");
}

public class A {
	public void some1() { }
	public virtual void some2() {}
	public virtual void print(string text) {
		log(text+" ==> A");
	}
}

public class B : A {
	public void some1() { }	        // WARN: use "new" if hiding was intended.
	public virtual void some2() {}	// WARN: hides inherited member; use "override" or "new"
	public new void some3() { }     // WARN: doesn't hide anything, "new" not required.
	public override void print(string text) {
		log(text+" ==> B");
	}
}

public class C : A {
	public new void print(string text) {
		log(text+" ==> C");
	}
}


Executing "Test" generates the following output:

A=new A ==> A
B=new B ==> B
C=new C ==> C
A=new B ==> B  <-- inheritance
A=new C ==> A  <-- no inheritance


illustrating the difference between override and new.

Luc Pattyn [Forum Guidelines] [My Articles]

The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
Show formatted code inside PRE tags, and give clear symptoms when describing a problem.

GeneralRe: Using the New keyword Pin
Mycroft Holmes12-May-09 19:06
professionalMycroft Holmes12-May-09 19:06 
GeneralRe: Using the New keyword Pin
PIEBALDconsult12-May-09 17:34
mvePIEBALDconsult12-May-09 17:34 
GeneralRe: Using the New keyword Pin
N a v a n e e t h12-May-09 18:15
N a v a n e e t h12-May-09 18:15 
GeneralRe: Using the New keyword Pin
PIEBALDconsult13-May-09 3:21
mvePIEBALDconsult13-May-09 3:21 
GeneralRe: Using the New keyword Pin
Mycroft Holmes12-May-09 19:09
professionalMycroft Holmes12-May-09 19:09 
GeneralRe: Using the New keyword Pin
Luc Pattyn12-May-09 23:58
sitebuilderLuc Pattyn12-May-09 23:58 
QuestionRetrieving USB Barcode scanner data... Pin
Jacob Dixon12-May-09 14:58
Jacob Dixon12-May-09 14:58 
AnswerRe: Retrieving USB Barcode scanner data... Pin
Mycroft Holmes12-May-09 15:28
professionalMycroft Holmes12-May-09 15:28 
GeneralRe: Retrieving USB Barcode scanner data... Pin
Jacob Dixon12-May-09 15:54
Jacob Dixon12-May-09 15:54 
GeneralRe: Retrieving USB Barcode scanner data... Pin
Luc Pattyn12-May-09 17:12
sitebuilderLuc Pattyn12-May-09 17:12 
AnswerRe: Retrieving USB Barcode scanner data... Pin
Dave Kreskowiak12-May-09 17:19
mveDave Kreskowiak12-May-09 17:19 
GeneralRe: Retrieving USB Barcode scanner data... Pin
Mycroft Holmes12-May-09 19:07
professionalMycroft Holmes12-May-09 19:07 
GeneralRe: Retrieving USB Barcode scanner data... Pin
Jacob Dixon13-May-09 3:06
Jacob Dixon13-May-09 3:06 
AnswerRe: Retrieving USB Barcode scanner data... Pin
cackharot27-May-09 15:03
cackharot27-May-09 15:03 
GeneralRe: Retrieving USB Barcode scanner data... Pin
Jacob Dixon28-May-09 4:17
Jacob Dixon28-May-09 4:17 
QuestionSending file via TCP protocol Pin
nike_arh12-May-09 10:31
nike_arh12-May-09 10:31 
AnswerRe: Sending file via TCP protocol Pin
Luc Pattyn12-May-09 14:11
sitebuilderLuc Pattyn12-May-09 14:11 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.