abstarct class basic question [modified]
-
Hi is in the following example is possible to add method Create() directly in the abstract class instead of in each derived class (basically the methos do the same: return inizialide class of its type). Something like this, that it doesn't works: abstract class b { public b Create() { { return new b(); } // but can not understand the derived class type } } Hope to be clear Thanks for your time
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace general
{class Program { static void Main(string\[\] args) { D1 myClass = new D1(); D1 newClass = myClass.Create(); //this is what I would like //b myClassAb = new D1(); //b newClassAb = myClassAb.Create(); } } abstract class b { //do no work in this way !!! //public b Create() { return new b(); } // but can not understand the derived class type } class D1:b { public double KM ; public D1() { KM = 1.0; } public D1 Create() { return new D1(); } } class D2 : b { public double KM ; public D2() { KM = 2.0; } public D2 Create() { return new D2(); } }
}
modified on Wednesday, June 1, 2011 3:15 AM
-
Hi is in the following example is possible to add method Create() directly in the abstract class instead of in each derived class (basically the methos do the same: return inizialide class of its type). Something like this, that it doesn't works: abstract class b { public b Create() { { return new b(); } // but can not understand the derived class type } } Hope to be clear Thanks for your time
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace general
{class Program { static void Main(string\[\] args) { D1 myClass = new D1(); D1 newClass = myClass.Create(); //this is what I would like //b myClassAb = new D1(); //b newClassAb = myClassAb.Create(); } } abstract class b { //do no work in this way !!! //public b Create() { return new b(); } // but can not understand the derived class type } class D1:b { public double KM ; public D1() { KM = 1.0; } public D1 Create() { return new D1(); } } class D2 : b { public double KM ; public D2() { KM = 2.0; } public D2 Create() { return new D2(); } }
}
modified on Wednesday, June 1, 2011 3:15 AM
-
abstract class b { public dynamic Create() { return Activator.CreateInstance(this.GetType()); } }
"You get that on the big jobs."
Thanks a lot It works . The only constrain is that you need VS10 to use "dymanic". Is there an old workaround?
-
Thanks a lot It works . The only constrain is that you need VS10 to use "dymanic". Is there an old workaround?
I don't think you need dynamic here. You know what type the object will be: it will be a
b
.public virtual b Create() { return (b)Activator.CreateInstance(GetType()); }
It's probably best to make this method virtual so that subclasses can do something different if they need to. This is quite an unusual thing to want to do unless you are trying to copy an object:
void SomeFunction(b instance){
b copy = instance.Create();
instance.CopyTo(copy);
}... as it requires you to have an instance of the object to create the new one from.
-
I don't think you need dynamic here. You know what type the object will be: it will be a
b
.public virtual b Create() { return (b)Activator.CreateInstance(GetType()); }
It's probably best to make this method virtual so that subclasses can do something different if they need to. This is quite an unusual thing to want to do unless you are trying to copy an object:
void SomeFunction(b instance){
b copy = instance.Create();
instance.CopyTo(copy);
}... as it requires you to have an instance of the object to create the new one from.
Thanks a lot! So suppose I do the following,
abstract class b
{
public virtual b Create() { return (b)Activator.CreateInstance(GetType()); }
}I'm not able to use method of the derived class?
class Program
{
static void Main(string[] args)
{
D1 myClass = new D1();
D1 newClass = myClass.Create();
Console.WriteLine(newClass.KM);// Bur b myClassAb = new D1(); b newClassAb = myClassAb.Create(); Console.WriteLine(newClassAb.KM);//KM is not avaiable :( } }
-
Thanks a lot! So suppose I do the following,
abstract class b
{
public virtual b Create() { return (b)Activator.CreateInstance(GetType()); }
}I'm not able to use method of the derived class?
class Program
{
static void Main(string[] args)
{
D1 myClass = new D1();
D1 newClass = myClass.Create();
Console.WriteLine(newClass.KM);// Bur b myClassAb = new D1(); b newClassAb = myClassAb.Create(); Console.WriteLine(newClassAb.KM);//KM is not avaiable :( } }
That's correct (unless you cast it). That's kind of an unavoidable consequence of putting any method on the base class. Typically in an abstract class hierarchy, the base class will define (either concrete or abstract) most of the public interface, so you can do most things through the base. If you know that you are working with D1, after all, you can just create a new instance with
new D1()
. The dynamic Create method is only needed when you want a new instance of the same type as another object, and you don't know what type that is, so you wouldn't be able to call class-specific functionality on it anyway. -
That's correct (unless you cast it). That's kind of an unavoidable consequence of putting any method on the base class. Typically in an abstract class hierarchy, the base class will define (either concrete or abstract) most of the public interface, so you can do most things through the base. If you know that you are working with D1, after all, you can just create a new instance with
new D1()
. The dynamic Create method is only needed when you want a new instance of the same type as another object, and you don't know what type that is, so you wouldn't be able to call class-specific functionality on it anyway.Thank you for your clear answer. So:
b myClassAb = new D1();
b newClassAb = myClassAb.Create();even if newClassAb is a D1 type, I cannot use D1 methods unless cast it. So no way to avoid to write the same piece of code in each derived class even if they basically do the same thing, if I want to use all methods of derived class. Is it?
class D1:b
{
..
public D1 Create() { return new D1(); }
}
class D2 : b
{
..
public D2 Create() { return new D2(); }
} -
Thank you for your clear answer. So:
b myClassAb = new D1();
b newClassAb = myClassAb.Create();even if newClassAb is a D1 type, I cannot use D1 methods unless cast it. So no way to avoid to write the same piece of code in each derived class even if they basically do the same thing, if I want to use all methods of derived class. Is it?
class D1:b
{
..
public D1 Create() { return new D1(); }
}
class D2 : b
{
..
public D2 Create() { return new D2(); }
}You can use generics like this:
abstract class b<T> where T: b, new()
{
...
public T Create() { return new T(); }
}class D1 : b<D1>
{
...
}
class D2 : b<D2>
{
...
}So, you can do:
D1 myClassD1 = new D1();
D1 newClassD1 = D1.Create();But, you can't do anymore:
b myClassAb = new D1;
D1 newClassAb = myClassAb.Create();because you'd have to specify the generic type for class b.
-
Hi is in the following example is possible to add method Create() directly in the abstract class instead of in each derived class (basically the methos do the same: return inizialide class of its type). Something like this, that it doesn't works: abstract class b { public b Create() { { return new b(); } // but can not understand the derived class type } } Hope to be clear Thanks for your time
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace general
{class Program { static void Main(string\[\] args) { D1 myClass = new D1(); D1 newClass = myClass.Create(); //this is what I would like //b myClassAb = new D1(); //b newClassAb = myClassAb.Create(); } } abstract class b { //do no work in this way !!! //public b Create() { return new b(); } // but can not understand the derived class type } class D1:b { public double KM ; public D1() { KM = 1.0; } public D1 Create() { return new D1(); } } class D2 : b { public double KM ; public D2() { KM = 2.0; } public D2 Create() { return new D2(); } }
}
modified on Wednesday, June 1, 2011 3:15 AM
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace general { class Program { static void Main(string[] args) { D1 myClass = new D1(); D1 newClass = myClass.Create(); //this is what I would like b myClassAb = new D1(); b newClassAb = myClassAb.Create<D1>(); newClass.KM = 1.23; } } abstract class b { //work in this way !!! public T Create<T>() where T:new() { return new T(); } } class D1 : b { public double KM; public D1() { KM = 1.0; } // public D1 Create() { return new D1(); } } class D2 : b { public double KM; public D2() { KM = 2.0; } // public D2 Create() { return new D2(); } } } here is one solution using generics
modified on Wednesday, June 1, 2011 9:53 AM
-
Thanks a lot It works . The only constrain is that you need VS10 to use "dymanic". Is there an old workaround?
Actually, you don't need Visual Studio at all. What you are meaning to say is "this requires .NET 4.0" :)
Luc Pattyn [My Articles] Nil Volentibus Arduum
The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
Please use <PRE> tags for code snippets, they improve readability.
CP Vanity has been updated to V2.3 -
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace general { class Program { static void Main(string[] args) { D1 myClass = new D1(); D1 newClass = myClass.Create(); //this is what I would like b myClassAb = new D1(); b newClassAb = myClassAb.Create<D1>(); newClass.KM = 1.23; } } abstract class b { //work in this way !!! public T Create<T>() where T:new() { return new T(); } } class D1 : b { public double KM; public D1() { KM = 1.0; } // public D1 Create() { return new D1(); } } class D2 : b { public double KM; public D2() { KM = 2.0; } // public D2 Create() { return new D2(); } } } here is one solution using generics
modified on Wednesday, June 1, 2011 9:53 AM
Sorry probably I did not explain very well the problem. I need to have avaiable methods in newClassAb //this is what I would like b myClassAb = new D1(); b newClassAb = myClassAb.Create<D1>(); //This is what I need to see K from newClassAb Console.WriteLine(newClassAb.K)>//but I can't see .K avaiable Thanks for your time
modified on Wednesday, June 1, 2011 7:32 AM
-
Thank you for your clear answer. So:
b myClassAb = new D1();
b newClassAb = myClassAb.Create();even if newClassAb is a D1 type, I cannot use D1 methods unless cast it. So no way to avoid to write the same piece of code in each derived class even if they basically do the same thing, if I want to use all methods of derived class. Is it?
class D1:b
{
..
public D1 Create() { return new D1(); }
}
class D2 : b
{
..
public D2 Create() { return new D2(); }
}There's a very simple way, if you know you want a D1: call
new D1()
notmyD1Instance.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.
-
You can use generics like this:
abstract class b<T> where T: b, new()
{
...
public T Create() { return new T(); }
}class D1 : b<D1>
{
...
}
class D2 : b<D2>
{
...
}So, you can do:
D1 myClassD1 = new D1();
D1 newClassD1 = D1.Create();But, you can't do anymore:
b myClassAb = new D1;
D1 newClassAb = myClassAb.Create();because you'd have to specify the generic type for class b.
-
Hi is in the following example is possible to add method Create() directly in the abstract class instead of in each derived class (basically the methos do the same: return inizialide class of its type). Something like this, that it doesn't works: abstract class b { public b Create() { { return new b(); } // but can not understand the derived class type } } Hope to be clear Thanks for your time
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace general
{class Program { static void Main(string\[\] args) { D1 myClass = new D1(); D1 newClass = myClass.Create(); //this is what I would like //b myClassAb = new D1(); //b newClassAb = myClassAb.Create(); } } abstract class b { //do no work in this way !!! //public b Create() { return new b(); } // but can not understand the derived class type } class D1:b { public double KM ; public D1() { KM = 1.0; } public D1 Create() { return new D1(); } } class D2 : b { public double KM ; public D2() { KM = 2.0; } public D2 Create() { return new D2(); } }
}
modified on Wednesday, June 1, 2011 3:15 AM
In response to your modification: KM should clearly be defined on the base class. It should probably also be a property.
abstract class b { public double KM { get; protected set; } // C# 2.0 automatic property syntax public b Create() { return Activator.CreateInstance(GetType()); } } class D1:b { public D1() { KM = 1.0; } } class D2 : b { public D2() { KM = 2.0; } }
-
In response to your modification: KM should clearly be defined on the base class. It should probably also be a property.
abstract class b { public double KM { get; protected set; } // C# 2.0 automatic property syntax public b Create() { return Activator.CreateInstance(GetType()); } } class D1:b { public D1() { KM = 1.0; } } class D2 : b { public D2() { KM = 2.0; } }
Console.WriteLine(b.//<big>b.KM is still not avaiable!!</big>}
namespace general
{class Program
{
static void Main(string[] args)
{//this is what I would like
b myClassAb = new D1();
b newClassAb = myClassAb.Create();
Console.WriteLine(b.//K is still not avaiable!!}
}abstract class b
{
public double KM { get; protected set; } // C# 2.0 automatic property syntax
public b Create() { return (b)Activator.CreateInstance(GetType()); }
}class D1 : b
{
public D1() { KM = 1.0; }
}
class D2 : b
{
public D2() { KM = 2.0; }
} -
Console.WriteLine(b.//<big>b.KM is still not avaiable!!</big>}
namespace general
{class Program
{
static void Main(string[] args)
{//this is what I would like
b myClassAb = new D1();
b newClassAb = myClassAb.Create();
Console.WriteLine(b.//K is still not avaiable!!}
}abstract class b
{
public double KM { get; protected set; } // C# 2.0 automatic property syntax
public b Create() { return (b)Activator.CreateInstance(GetType()); }
}class D1 : b
{
public D1() { KM = 1.0; }
}
class D2 : b
{
public D2() { KM = 2.0; }
}You should be able to use newClassAb.KM.
-
You should be able to use newClassAb.KM.
Great!!!! It works...Thanks a lot:cool:
-
Actually, you don't need Visual Studio at all. What you are meaning to say is "this requires .NET 4.0" :)
Luc Pattyn [My Articles] Nil Volentibus Arduum
The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
Please use <PRE> tags for code snippets, they improve readability.
CP Vanity has been updated to V2.3:thumbsup:
-
Sorry probably I did not explain very well the problem. I need to have avaiable methods in newClassAb //this is what I would like b myClassAb = new D1(); b newClassAb = myClassAb.Create<D1>(); //This is what I need to see K from newClassAb Console.WriteLine(newClassAb.K)>//but I can't see .K avaiable Thanks for your time
modified on Wednesday, June 1, 2011 7:32 AM
what is K is it KM well i can see KM try it once more because there was some error in my code