Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. abstarct class basic question [modified]

abstarct class basic question [modified]

Scheduled Pinned Locked Moved C#
csharplinqtutorialquestionlounge
25 Posts 8 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P PozzaVecia

    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; }
    }

    B Offline
    B Offline
    BobJanova
    wrote on last edited by
    #16

    You should be able to use newClassAb.KM.

    P 1 Reply Last reply
    0
    • B BobJanova

      You should be able to use newClassAb.KM.

      P Offline
      P Offline
      PozzaVecia
      wrote on last edited by
      #17

      Great!!!! It works...Thanks a lot:cool:

      1 Reply Last reply
      0
      • L Luc Pattyn

        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

        P Offline
        P Offline
        PIEBALDconsult
        wrote on last edited by
        #18

        :thumbsup:

        1 Reply Last reply
        0
        • B BobJanova

          Now you can't call Create on an arbitrary b instance, which renders it pointless.

          M Offline
          M Offline
          Mirko1980
          wrote on last edited by
          #19

          I said that at the end of my post. I agree you are losing many advatages of polymorphism, but, at least, you can reuse the Create login in multple classes.

          1 Reply Last reply
          0
          • P PozzaVecia

            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

            S Offline
            S Offline
            Steven Pinto2000
            wrote on last edited by
            #20

            what is K is it KM well i can see KM try it once more because there was some error in my code

            P 1 Reply Last reply
            0
            • S Steven Pinto2000

              what is K is it KM well i can see KM try it once more because there was some error in my code

              P Offline
              P Offline
              PozzaVecia
              wrote on last edited by
              #21

              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; }
              }
              }

              1 Reply Last reply
              0
              • P PozzaVecia

                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

                _ Offline
                _ Offline
                _Erik_
                wrote on last edited by
                #22

                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>();

                P 1 Reply Last reply
                0
                • _ _Erik_

                  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>();

                  P Offline
                  P Offline
                  PozzaVecia
                  wrote on last edited by
                  #23

                  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

                  _ 1 Reply Last reply
                  0
                  • P PozzaVecia

                    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

                    _ Offline
                    _ Offline
                    _Erik_
                    wrote on last edited by
                    #24

                    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)

                    P 1 Reply Last reply
                    0
                    • _ _Erik_

                      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)

                      P Offline
                      P Offline
                      PozzaVecia
                      wrote on last edited by
                      #25

                      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

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups