-1

Work on C# .I want to inherit generic class for this purpose i write the bellow syntax

 public class Father
    {
        public string FatherName { get; set; }
    }
    public class Mother
    {
        public string MotherName { get; set; }
    }

    public class Ancestors<T, U> 
    {
        //public class Bar
        //{
        //    private T t;
        //    private U u;
        //}
    }


    class Program : Ancestors<Father, Mother>
    {
        static void Main(string[] args)
        {
            Ansestors.Father.FatherName = "xxx";
        }
    }

I want Ansestors.Father.FatherName = "xxx"; property .What's problem on my syntax?Plz show some syntax to solve this issue.if have any query plz ask.Thanks in advance

shamim
  • 6,640
  • 20
  • 85
  • 151

3 Answers3

3

Looks like you haven't fully wrapped your head around the concept of generic classes.

What you say to the compiler with the above declaration is: Whenever you find a T (or U) in the code of the Program class, then replace it with Father (or Mother).

Why do you expect the Program class to have a nested object of type Father? It's not declared anywhere, all you've declared is a type resolving directive for the compiler.

HTH!

Thomas Weller
  • 11,631
  • 3
  • 26
  • 34
  • Thomas Weller you are right,I want to inherit two object on program class.I now C# does not support multiple inherit of class,but i need to inherit both class,From your post i don't understand why one class can not inherit generic class.will you plz tell me in detail or show some syntax for me.Thanks for reply. – shamim Oct 30 '11 at 17:18
  • Of course you can inherit from a generic class, that's not a problem. But what you inherit from a generic are purely type declarations, not objects. The multiple inheritance thing that you seem to have in mind has nothing to do with generics. You will need some sort of object composition. Inheritance in C# doesn't work for such things (thank god!). Looks like you should wrap your head around some software design principles... – Thomas Weller Oct 30 '11 at 18:31
2
  1. You should not inherit Program class, this is let's say entry point of an application
  2. Just create an instance of Ansestors<T, U> class, I've slightly refcatored Ancestor class, see below:
public interface IAncestor
{
    string Name { get; set; }
}

public sealed class Father : IAncestor
{
}

public sealed class Mother : IAncestor
{
}

public sealed class ParentsAncestor<T, U> 
 where T: IAncestor
 where U: IAncestor
{
     public ParentsAncestor(T father, U mother)
     {
         this.Father = father;
         this.Mother = mother;
     }

     public T Father { get; private set; }
     public U Mother { get; private set; } 
}

static void Main(string[] args)
{
     var instance = new ParentsAncestor<Father, Mother>(new Father(), new Mother());
     instance.Father.Name = "The father name";
}
Pharap
  • 3,826
  • 5
  • 37
  • 51
sll
  • 61,540
  • 22
  • 104
  • 156
  • 1
    Not really a problem but the varible names shouldn't be those as it would be misleading if they used `Ansestors;` – Ash Burlaczenko Oct 30 '11 at 16:50
  • @Ash Burlaczenko: good observation! I think they could be abstracted as FirstAncestor and SecondAncestor but it depends on context where this cass is used also – sll Oct 30 '11 at 17:07
  • BTW, in this way you do not het much flexibility using generics, you can get rid of them at all and just expose Father/Mother proeprties, you need generic type class argument when would share some common behaviour across a set of concrete implementation classes – sll Oct 30 '11 at 17:17
2
class Program
{
    static void Main(string[] args)
    {
        //original parents
        Father father = new Father("George");
        Mother mother = new Mother("Mary");

        //mothers parents aka grandparents
        mother.Mother = new Mother("Ana");
        mother.Father = new Father("Jack");
    }
}

abstract class Ancestor
{
    public String Name { get; set; }
}

public class Father : Ancestor {
    public Mother Mother { get; set; }
    public Father Father { get; set; }

    public Father(String name)
    {
        base.Name = name;
    }
}
public class Mother : Ancestor {
    public Mother Mother { get; set; }
    public Father Father { get; set; }

    public Mother(String name)
    {
        base.Name = name;
    }
}

You put common properties in Ancestor class, and specific properties in either Mother of Father class. I don't see why you need generics.

Btw, it makes no sense for Program class to inherit Ancestors...

BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
neeKo
  • 4,280
  • 23
  • 31