Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to specify a class that may contain another class

Tags:

c#

class

I want to create a class that have a class but this second class may be different each time the first class is called. For example:

public class ServerResponseObject
{
    public string statusCode { get; set; }
    public string errorCode { get; set; }
    public string errorDescription { get; set; }
    public Object obj { get; set; }

    public ServerResponseObject(Object obje)
    {
        obj = obje;
    }
}   

public class TVChannelObject
{
    public string number { get; set; }
    public string title { get; set; }
    public string FavoriteChannel { get; set; }
    public string description { get; set; }
    public string packageid { get; set; }
    public string format { get; set; }
}

public class ChannelCategoryObject
{
    public string id { get; set; }
    public string name { get; set; }
}

How can I do it to call the ServerResponseObject with different objects each time, once with TVChannelObject and once with ChannelCategoryObject?

like image 621
Maria Agaci Avatar asked Dec 18 '22 23:12

Maria Agaci


1 Answers

What you're looking for is a generic type parameter:

public class ServerResponseObject<T>
{
    public ServerResponseObject(T obj)
    {
        Obj = obj;
    }

    public T Obj { get; set; }
}
like image 181
Yuval Itzchakov Avatar answered Jan 16 '23 21:01

Yuval Itzchakov