Click here to Skip to main content
15,888,233 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I created a class of objects: text, number and list of pictures. I do not know if I'm doing well, but I wanted to store it in a list for a better using. Unfortunately, I do not know how to add a list to the list and how to show it later. Loop for? Can anyone help me with this?

What I have tried:

C#
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

<pre>namespace DynamicObjectList
{
    class Products
    {
        public int id { get; set; }
        public string title { get; set; }
        public string amount { get; set; }
        public string price { get; set; }
        public List<Image> images { get; set; }
	}
}
...
C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DynamicObjectList
{
    public partial class Form1 : Form
    {
        public int nr = 0;

        List<Products> prodList = new List<Products>(3);

        List<string> imgList1 = new List<string>();

        public Form1()
        {
            InitializeComponent();
        }

        private void button_Add_Click(object sender, EventArgs e)
        {
            nr++;
            Products prod = new Products();
            prod.id = nr;
            prod.title = "Product " + nr.ToString();
            prod.amount = (Convert.ToInt16("2") * nr).ToString();
            prod.price = (Convert.ToDouble("10,00") * nr).ToString();
            prod.images = imgList1;
            prodList.Add(prod);

        }

        private void button_Show_Click(object sender, EventArgs e)
        {


            for (int i = 0; i < prodList.Count; i++)
            {
                Products p = prodList[i];

                textBox_Wyswietla.Text += String.Format("ID: {0}, Title: {1}, Amount: {2}, Price: {3}\r\n", p.id, p.title, p.amount, p.price);

                List<string> img = new List<string>();
                img = p.images;
                for (int j = 0; j < img.Count; j++)
                {
                    textBox_Wyswietla.Text += img[j] + "\r\n";
                }
            }

        }
...
Posted
Updated 4-May-18 0:54am
Comments
F-ES Sitecore 4-May-18 6:40am    
You add a List to a List using AddRange

mainList.AddRange(listToAdd);

The List class is strongly typed, which means it only accepts objects which are of the same class at the list was originally defined to hold (or which are derived from that class).
For example:
C#
class A {}
class B : A {}
class C : B {}
List<A> dataA = new List<A>();
List<B> dataB = new List<B>();
List<C> dataC = new List<C>();
dataA can hold instances of classes A , B, and C; dataB can contain instances of B and C, but not A; and dataC can only hold instances of C, but not A or B.

So you can't create a List<Products> and expect it to hold anything other than Products instances - and you certainly can't add a List<Products> to it as they are not even slightly related!

If what you are trying to do is add a different List of Images (which is actually a List of strings in your example) to each Products (and please, do us all a favour and get rid of the "s" there - it implies it's a collection class instead of a single object) then you are halfway there - all you need to do is create a new instance of the images list when you create the Product:
Products prod = new Products();
prod.id = nr;
prod.title = "Product " + nr.ToString();
prod.amount = (Convert.ToInt16("2") * nr).ToString();
prod.price = (Convert.ToDouble("10,00") * nr).ToString();
prod.images = new List<string>(imgList1);
imgList1.Clear();
(The Clear to to remove the items for the next time)
 
Share this answer
 
Hello,

check this C# 4 - Tuples[^]

This will help you.

--RA
 
Share this answer
 
Comments
Afzaal Ahmad Zeeshan 4-May-18 11:29am    
Consider posting link references as comments, unless they are meant to be solutions and they solve the problem too.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900