Home > other >  How to read and print an Array List in a list?
How to read and print an Array List in a list?

Time:03-31

I having trouble printing an ArrayList correctly. While debugging I can see that it reads it correctly, but in richTextBox1(finalResults) it prints it as System.Collections.ArrayList

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;
using System.IO;
using System.Collections;

namespace Laboratorinis_P3
{
    public partial class Form1 : Form
    {
        List<Museum> firstList;
        List<Museum> newList;
        List<Museum> twoList;
        public Form1()
        {
            InitializeComponent();
        }
        private void formListBySelectedCityToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string city = Convert.ToString(Cities.SelectedItem);
            LinqForming(city);
           
        }
        private void findTwoMuseumsToolStripMenuItem_Click(object sender, EventArgs e)
        {
           
        }
        private void readFileAndPrintItToolStripMenuItem_Click(object sender, EventArgs e)
        {
          
            OpenFileDialog openFileDialog1 = new OpenFileDialog();
            openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
            openFileDialog1.Title = "Pasirinkite duomenų failą";
            DialogResult result = openFileDialog1.ShowDialog();
            if (result == DialogResult.OK)
            {
                string path = openFileDialog1.FileName;
                firstList = ReadFile(path,firstList);
                PrintList(firstList);
                AddCitiesToComboBox(firstList);
            }
 
        }
       
        static List<Museum> ReadFile(string fv,List<Museum> firstList)
        {
            ArrayList days = new ArrayList();
            firstList = new List<Museum>();
            using (StreamReader reader = new StreamReader(fv))
            {

                string line;
                
                while ((line = reader.ReadLine()) != null)
                {

                    string[] parts = line.Split(";");
                    string name = parts[0];
                    string city = parts[1];
                    string type = parts[2];
                    string[] day = parts[3].Trim().Split(new[] {' '});
                    days.Clear();
                    foreach (string lines in day)
                    {
                        int dayss = int.Parse(lines);
                        days.Add(dayss);
                    }

                    double adult = double.Parse(parts[4]);
                    double kid = double.Parse(parts[5]);
                    string hasguide = parts[6];
                    Museums museum = new Museums(name, city, type, days, adult, kid, hasguide);
                    firstList.Add(museum);

                }
            }
            return firstList;
        }
        private void LinqForming(string city)
        {
            newList = firstList                     
                      .Where(x => x.City == city)
                      .ToList();
        }
       
        private void AddCitiesToComboBox(List<Museum> list)
        {
            for (int i = 0; i < list.Count; i  )
            {
                if (!Cities.Items.Contains(list[i].City))
                {
                    Cities.Items.Add(list[i].City);
                }
            }
        }
        private void PrintList(List<Museum> museum)
        {
            for (int i = 0; i < museum.Count; i  )
            {
                finalResults.Text  = museum[i].ToString();
            }
        }
    }
}

using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;

namespace Laboratorinis_P3
{
    class Museum
    {
        public string Name { get; set; }
        public string City { get; set; }
        public string MuseumTypes { get; set; }

        public ArrayList Days { get; set; }
        public double PriceForAdult { get; set; }
        public double PriceForKid { get; set; }
        public string HasGuide { get; set; }

        public Museum()
        {

        }

        public Museum(string name, string city, string type, ArrayList days,
                      double adult, double kid, string hasguide)
        {
            this.Name = name;
            this.City = city;
            this.MuseumTypes = type;
            this.Days = days;
            this.PriceForAdult = adult;
            this.PriceForKid = kid;
            this.HasGuide = hasguide;
        }

        public override string ToString()
        {
            string eilute;
            eilute = string.Format
                ("| {0,5}|{1,13}|{2,10} |{3,9}|{4,12}|{5,13}|{6,10} |",Name,City,MuseumTypes,
                 Days,PriceForAdult,PriceForKid,HasGuide);
                           
            return eilute;
        }
    }
}

Data file example

Gato;Vilnius;Elektros; 1 2 3 ;8.5;4.5;turi
Miau;Vilnius;Gyvunai; 1 2 ;11.5;2.5;neturi
AuAu;Siauliai;Biologija; 1 2 3;5.0;4.75;turi
GuGa;Panevezys;Menas; 1 2 3;12.15;1.78;turi

I tried everything that I could think up with to print it as it should but it does not seem to work. To add some explanation: the numbers are equal to days, which I am trying to print

CodePudding user response:

You could use String.Join() to put your days together, then insert that into your output string:

public override string ToString()
{
    string eilute;
    string strDays = String.Join(",", Days.ToArray());
    eilute = string.Format
        ("| {0,5}|{1,13}|{2,10} |{3,9}|{4,12}|{5,13}|{6,10} |", Name, City, MuseumTypes,
         strDays, PriceForAdult, PriceForKid, HasGuide);

    return eilute;
}

CodePudding user response:

finalResults.Text  = museum[i];

museum is an object (not a text value), so you cannot append it directly to finalResults.Text

You should modify it like this

finalResults.Text  = museum[i].Name   museum[i].City; //add any fields you want to have in the form

CodePudding user response:

The Days field type is ArrayList and that is why you are getting

;System.Collections.ArrayList;

change your code to:

eilute = string.Format
            ("| {0,5}|{1,13}|{2,10} |{3,9}|{4,12}|{5,13}|{6,10} |",Name,City,MuseumTypes,
             String.Join(" ", Days.ToArray().Select(s => s.ToString())),PriceForAdult,PriceForKid,HasGuide);
  •  Tags:  
  • c#
  • Related