locked
Pass generic in method parameter? RRS feed

  • Question

  • Hi,

    I have class Tour inherit from class Chromosom

    public abstract class Chromosom {}
    
    public class Tour: Chromosom

    and I also have class IFitness using Chromosom als Parameter

    interface IFitness
    {
        double Calculate(Chromosom chromosom);
    }
    
    


    Is it possible to have something like this

    public class Fitness : IFitness
    {
          public double Calculate(Tour tour)
          {
          }
    }

    instead of

    public class Fitness : IFitness
    {
          public double Calculate(Chromosom chromosom)
          {
          }
    }

    using generics maybe?

    Many thanks :)

    Sunday, December 9, 2012 2:30 PM

Answers

  • Try a structure like this:

      public abstract class Chromosum {}
    
      public class Tour : Chromosum {}
    
      public interface IFitness<T> where T : Chromosum {
        double Calculate(T t);
      }
    
      public class FitnessTour : IFitness<Tour> {
        public double Calculate(Tour tour) {
          return 0D;
        }
    
        public double RunIt() {
          return Calculate(new Tour());
        }
      }
    


    "Premature optimization is the root of all evil." - Knuth

    If I provoked thought, please click the green arrow

    If I provoked Aha! please click Propose as Answer

    We are here to learn, to share knowledge, and to earn points; all in about equal measure.

    • Proposed as answer by Md. Marufuzzaman Sunday, December 9, 2012 3:30 PM
    • Marked as answer by Lisa Zhu Tuesday, December 18, 2012 10:38 AM
    Sunday, December 9, 2012 2:49 PM

All replies

  • Try a structure like this:

      public abstract class Chromosum {}
    
      public class Tour : Chromosum {}
    
      public interface IFitness<T> where T : Chromosum {
        double Calculate(T t);
      }
    
      public class FitnessTour : IFitness<Tour> {
        public double Calculate(Tour tour) {
          return 0D;
        }
    
        public double RunIt() {
          return Calculate(new Tour());
        }
      }
    


    "Premature optimization is the root of all evil." - Knuth

    If I provoked thought, please click the green arrow

    If I provoked Aha! please click Propose as Answer

    We are here to learn, to share knowledge, and to earn points; all in about equal measure.

    • Proposed as answer by Md. Marufuzzaman Sunday, December 9, 2012 3:30 PM
    • Marked as answer by Lisa Zhu Tuesday, December 18, 2012 10:38 AM
    Sunday, December 9, 2012 2:49 PM
  • Wow this is what I am looking for. Thank you :)

    Sunday, December 9, 2012 3:06 PM
  • I did move the type parameter from the method Calculate to the interface IFitness, which is usually (but not always) a better match to requirements.

    "Premature optimization is the root of all evil." - Knuth

    If I provoked thought, please click the green arrow

    If I provoked Aha! please click Propose as Answer

    We are here to learn, to share knowledge, and to earn points; all in about equal measure.

    Sunday, December 9, 2012 3:17 PM