abstarct class basic question [modified]
-
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
-
what is K is it KM well i can see KM try it once more because there was some error in my code
b myClassAb = new D1();
b newClassAb = myClassAb.Create<D1>();
newClassAb.KM = 1.23;apparently return this error" Error 1 'general.b' does not contain a definition for 'KM' and no extension method 'KM' accepting a first argument of type 'general.b' could be found (are you missing a using directive or an assembly reference?)" The working solution example, thanks to BobJanova is:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;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(newClassAb.KM);
}
}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; }
}
} -
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
I am sorry but, what is the point in creating an instance of a class to just create another instance of the same class? I mean, since Create method is not static it would just work as a constructor so, why don't you just use a constructor? For example, what you want to do is this:
D1 obj = new D1();
D1 obj2 = D1.Create();And what I am saying is... why not?
D1 obj = new D1();
D1 obj2 = new D1();I guess you want a static Create method in your base class, don't you? In this case you will have to make your Create method static and use generics:
abstract class BaseClass
{
public static T Create<T>() where T : BaseClass, new()
{
return new T();
}
}class DerivedClass : BaseClass { }
This way you could use it like this:
DerivedClass obj = BaseClass.Create<DerivedClass>();
-
I am sorry but, what is the point in creating an instance of a class to just create another instance of the same class? I mean, since Create method is not static it would just work as a constructor so, why don't you just use a constructor? For example, what you want to do is this:
D1 obj = new D1();
D1 obj2 = D1.Create();And what I am saying is... why not?
D1 obj = new D1();
D1 obj2 = new D1();I guess you want a static Create method in your base class, don't you? In this case you will have to make your Create method static and use generics:
abstract class BaseClass
{
public static T Create<T>() where T : BaseClass, new()
{
return new T();
}
}class DerivedClass : BaseClass { }
This way you could use it like this:
DerivedClass obj = BaseClass.Create<DerivedClass>();
I was looking for a way to avoid to write the same piece of code in each derived class since they basically do the same thing. I Would like to create a method in the base class. But this should allow you to use methods of derived class Is it?
class D1:b
{
..
public D1 Create() { return new D1(); }
}
class D2 : b
{
..
public D2 Create() { return new D2(); }
}So my goal is not to do
D1 obj = new D1();
D1 obj2 = D1.Create();but to do
b myClassAb = new D1();
b newClassAb = myClassAb.Create();
Console.WriteLine(newClassAb.KM); //i.e. I need to access to proprieties, methods, etc of class -
I was looking for a way to avoid to write the same piece of code in each derived class since they basically do the same thing. I Would like to create a method in the base class. But this should allow you to use methods of derived class Is it?
class D1:b
{
..
public D1 Create() { return new D1(); }
}
class D2 : b
{
..
public D2 Create() { return new D2(); }
}So my goal is not to do
D1 obj = new D1();
D1 obj2 = D1.Create();but to do
b myClassAb = new D1();
b newClassAb = myClassAb.Create();
Console.WriteLine(newClassAb.KM); //i.e. I need to access to proprieties, methods, etc of classMember 4282287 wrote:
I need to access to proprieties, methods, etc of class
That does not depend on how you create the objects, I mean, your Create method will not help you to achieve what you want. If you can access KM property of a D1 object which you have declared as its base class type (b in this case), you can becouse KM property is definied within the "b" class interface, but not becouse you have made such a strange Create method. So in your sample:
b myClassAb = new D1();
b newClassAb = myClassAb.Create();
Console.WriteLine(newClassAb.KM)You don't need the Create method at all. Just use the constructor and one instance of the object:
b myClassAb = new D1();
Console.WriteLine(myClassAb.KM) -
Member 4282287 wrote:
I need to access to proprieties, methods, etc of class
That does not depend on how you create the objects, I mean, your Create method will not help you to achieve what you want. If you can access KM property of a D1 object which you have declared as its base class type (b in this case), you can becouse KM property is definied within the "b" class interface, but not becouse you have made such a strange Create method. So in your sample:
b myClassAb = new D1();
b newClassAb = myClassAb.Create();
Console.WriteLine(newClassAb.KM)You don't need the Create method at all. Just use the constructor and one instance of the object:
b myClassAb = new D1();
Console.WriteLine(myClassAb.KM)ok thanks understood. Really the starting example was a bit different ... i.e. I needed a method avaiable only in derived class. During the forum this method ... become a proprierties.. Apparently I understood. You are right of course. Thanks for your time