Click here to Skip to main content
15,913,330 members
Home / Discussions / C#
   

C#

 
GeneralRe: abstarct class basic question Pin
BobJanova31-May-11 23:03
BobJanova31-May-11 23:03 
GeneralRe: abstarct class basic question Pin
PozzaVecia31-May-11 23:28
PozzaVecia31-May-11 23:28 
GeneralRe: abstarct class basic question Pin
BobJanova31-May-11 23:33
BobJanova31-May-11 23:33 
GeneralRe: abstarct class basic question Pin
PozzaVecia31-May-11 23:45
PozzaVecia31-May-11 23:45 
GeneralRe: abstarct class basic question Pin
Mirko19801-Jun-11 0:24
Mirko19801-Jun-11 0:24 
GeneralRe: abstarct class basic question Pin
BobJanova1-Jun-11 1:37
BobJanova1-Jun-11 1:37 
GeneralRe: abstarct class basic question Pin
Mirko19801-Jun-11 3:24
Mirko19801-Jun-11 3:24 
GeneralRe: abstarct class basic question Pin
BobJanova1-Jun-11 1:34
BobJanova1-Jun-11 1:34 
There's a very simple way, if you know you want a D1: call new D1() not myD1Instance.Create().

I'm guessing this is a simplified example, because it simply doesn't make sense to do things this way in most cases. What is your actual problem?

Accepting for the moment that you need a Create method at all: If you know that an instance of b is actually a D1 (for example within the D1 class itself) then you can cast the result of Create safely. Casting is not necessarily bad (it's safer than the use of dynamic, I would say). For example, in some similar code to what I have at the moment for cloning objects in an inheritance tree:

class b {
 private int someField;

 public b Copy() {
  b newB = Create();
  CopyTo(newB);
  return newB;
 }
 
 protected virtual void CopyTo(b newB){
  // copy properties into the target
  newB.someField = someField;
 }

class D1 : b {
 private double derivedField;

 protected override void CopyTo(b newD1){
  // This method will only be called if we are a D1,
  // and therefore the result of Create is also a D1,
  // and therefore the parameter is also a D1
  D1 target = (D1)newD1; // ... so this must succeed
  target.derivedField = derivedField; // ... and we can access D1's members
 }
}


Outside the class tree, it's unlikely that you know for sure at compile time which of the classes you have an instance of (otherwise you wouldn't need the abstract base class at all), so you can normally only cast the result inside the class, or using if(my is D1) type tests. And if you find yourself writing those you probably just found somewhere that an abstract method should be provided on the base class.
AnswerRe: abstarct class basic question Pin
Luc Pattyn1-Jun-11 0:46
sitebuilderLuc Pattyn1-Jun-11 0:46 
GeneralRe: abstarct class basic question Pin
PIEBALDconsult1-Jun-11 2:57
mvePIEBALDconsult1-Jun-11 2:57 
AnswerRe: abstarct class basic question [modified] Pin
Steven.Pinto20001-Jun-11 0:28
Steven.Pinto20001-Jun-11 0:28 
GeneralRe: abstarct class basic question [modified] Pin
PozzaVecia1-Jun-11 1:03
PozzaVecia1-Jun-11 1:03 
GeneralRe: abstarct class basic question Pin
Steven.Pinto20001-Jun-11 3:52
Steven.Pinto20001-Jun-11 3:52 
GeneralRe: abstarct class basic question Pin
PozzaVecia1-Jun-11 4:03
PozzaVecia1-Jun-11 4:03 
AnswerRe: abstarct class basic question Pin
BobJanova1-Jun-11 1:40
BobJanova1-Jun-11 1:40 
GeneralRe: abstarct class basic question Pin
PozzaVecia1-Jun-11 1:52
PozzaVecia1-Jun-11 1:52 
GeneralRe: abstarct class basic question Pin
BobJanova1-Jun-11 1:53
BobJanova1-Jun-11 1:53 
GeneralRe: abstarct class basic question Pin
PozzaVecia1-Jun-11 2:09
PozzaVecia1-Jun-11 2:09 
AnswerRe: abstarct class basic question Pin
_Erik_1-Jun-11 4:10
_Erik_1-Jun-11 4:10 
GeneralRe: abstarct class basic question Pin
PozzaVecia1-Jun-11 5:24
PozzaVecia1-Jun-11 5:24 
GeneralRe: abstarct class basic question Pin
_Erik_2-Jun-11 2:00
_Erik_2-Jun-11 2:00 
GeneralRe: abstarct class basic question Pin
PozzaVecia2-Jun-11 2:11
PozzaVecia2-Jun-11 2:11 
QuestionC# COM Interop Pin
Vijjuuu.31-May-11 19:15
Vijjuuu.31-May-11 19:15 
AnswerRe: C# COM Interop Pin
BobJanova31-May-11 23:05
BobJanova31-May-11 23:05 
AnswerRe: C# COM Interop Pin
Mirko19801-Jun-11 0:12
Mirko19801-Jun-11 0:12 

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.