M Fayyaz
M Fayyaz

Reputation: 165

How to create a List from user defined Classes

Hello everyone I am new to Stackoverflow so ignore the mistakes. I have different user defined classes which have number of attributes. I want to create a List with the use of these classes and need to use system define datatype instead of user define classes... Here is the code you can better understand with it.

Here are the classes

public class Slide
{
    public string Name { get; set; }
    public bool IsChecked { get; set; }
}
//.........
public class SubSection
{
    public SubSection() 
    { 
        this.Composition = new ObservableCollection<object>();
    }
    public string Name { get; set; }
    public bool IsChecked { get; set; }
    public ObservableCollection<object> Composition { get; set; }

}
//................
public class Section
{
    public Section()
    {
        this.SubSections = new List<SubSection>();
    }
    public string Name { get; set; }
    public bool IsChecked { get; set; }
    public List<SubSection> SubSections { get; set; }
}

Every Node in the list should have section,subsection and slide

Upvotes: 2

Views: 3116

Answers (3)

Arkiliknam
Arkiliknam

Reputation: 1825

I would agree with Josh Smeaton's answer of Tuples, but for fun I wonder if you could consider Anonymous Types as a System Defined Type...?

var myList = new[]
{
    new { Section = new Section(), SubSection = new SubSection(), Slide = new Slide()}, 
    new { Section = new Section(), SubSection = new SubSection(), Slide = new Slide()}
}.ToList();

Upvotes: 0

Josh Smeaton
Josh Smeaton

Reputation: 48710

I'm assuming that you need a list, where every element in the list contains one of each of the classes you've listed in your question. You can use a List of Tuples:

var mylist = new List<Tuple<Section, SubSection, Slide>>();
mylist.Add(Tuple.Create(new Section(), new SubSection(), new Slide());
mylist.Add(Tuple.Create(new Section(), new SubSection(), new Slide());
mylist.Add(Tuple.Create(new Section(), new SubSection(), new Slide());

Tuples were introduced in .NET 4.5, so as long as you're on at least 4.5, this will work for you.

Upvotes: 1

Hossain Muctadir
Hossain Muctadir

Reputation: 3626

First create a model class with all your data you want to include and after that you can create list of that class.

public class CustomClass
{
   public Section{get;set;}
   public SubSection{get;set;}
   public Slide{get;set;}
}

var customClasses = new List<CustomClass>();

Upvotes: 0

Related Questions