C# using Genetics

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • yootaeho@gmail.com

    #1

    C# using Genetics

    Hi,

    I have an Object which has a list of list of objects.
    For instance,

    public class myClass
    {
    public List<FirstClass myFirstClass {get;set;}
    public List<SecondClas smySecondClass {get;set;}
    }

    And both FirstClass and SecondClass have Id and Title properties.

    Now I would like to create a function that takes a genetic list type
    and then inside the function I would like to access Id and Title value

    So something like this

    public void myFunction (genetic variable)
    {
    // here I would like to access variable's Id and Title
    }


    myFunction(myCl ass.myFirstClas s);
    myFunction(myCl ass.mySecondCla ss);

    Would you be able to help me to write the function me?

    Thanks in advance,

    Taeho
  • Peter Duniho

    #2
    Re: C# using Genetics

    On Thu, 30 Oct 2008 15:49:37 -0700, <yootaeho@gmail .comwrote:
    I have an Object which has a list of list of objects.
    For instance,
    >
    public class myClass
    {
    public List<FirstClass myFirstClass {get;set;}
    public List<SecondClas smySecondClass {get;set;}
    }
    >
    And both FirstClass and SecondClass have Id and Title properties.
    Do they both inherit/implement a common class or interface with those
    properties?

    If not, you'll have to wait for the release of C# 4.0, which includes a
    new "dynamic" type allowing for late-binding of class members.

    If they do inherit/implement a common class or interface, then you can do
    what you want with generics as they are now (by the way, note that it's
    "generic" not "genetic"). Specifically, let's assume that they both
    implement an interface that looks like this:

    interface ICommon
    {
    int Id { get; set; }
    string Title { get; set; }
    }

    And the class declarations look something like this:

    class FirstClass : ICommon { ... }
    class SecondClass : ICommon { ... }

    Then, you can write the desired method like this:

    void myFunction<T>(L ist<Tvariable) where T : ICommon
    {
    }

    A method like that can take as an argument any List<Twhere the T
    implements ICommon. So a List<FirstClass or a List<SecondClas swould be
    valid.

    Pete

    Comment

    • yootaeho@gmail.com

      #3
      Re: C# using Genetics

      On Oct 31, 10:31 am, "Peter Duniho" <NpOeStPe...@nn owslpianmk.com>
      wrote:
      On Thu, 30 Oct 2008 15:49:37 -0700, <yoota...@gmail .comwrote:
      I have an Object which has a list of list of objects.
      For instance,
      >
      public class myClass
      {
           public List<FirstClass myFirstClass {get;set;}
           public List<SecondClas smySecondClass {get;set;}
      }
      >
      And both FirstClass and SecondClass have Id and Title properties.
      >
      Do they both inherit/implement a common class or interface with those  
      properties?
      >
      If not, you'll have to wait for the release of C# 4.0, which includes a  
      new "dynamic" type allowing for late-binding of class members.
      >
      If they do inherit/implement a common class or interface, then you can do 
      what you want with generics as they are now (by the way, note that it's  
      "generic" not "genetic").  Specifically, let's assume that they both  
      implement an interface that looks like this:
      >
           interface ICommon
           {
               int Id { get; set; }
               string Title { get; set; }
           }
      >
      And the class declarations look something like this:
      >
           class FirstClass : ICommon { ... }
           class SecondClass : ICommon { ... }
      >
      Then, you can write the desired method like this:
      >
           void myFunction<T>(L ist<Tvariable) where T : ICommon
           {
           }
      >
      A method like that can take as an argument any List<Twhere the T  
      implements ICommon.  So a List<FirstClass or a List<SecondClas swouldbe  
      valid.
      >
      Pete
      Thank you very much for your help!!!

      Comment

      Working...