I have been learning about the collections namespace in C# so have been doing some little self-made projects to learn.

I have made a car race app where you enter in the name, car and place of the racer this is then stored in an array list. I can extract the contents of my array list onto the console window but I would like to know how I can write and save it to a text file (kindof like a mini database).

I know that every element stored in an array list is stored as an object therefore casting is needed to extract them to their appropriate types.

Here is how my array list handles the inputs:

class People
    {
        private string name;
        private string car;
        private string place;
        public ArrayList myList = new ArrayList();

        public string Name
        {
            get { return name; }
            set { name = value; }
        }

        public string Car
        {
            get { return car; }
            set { car = value; }
        }

        public string Place
        {
            get { return place; }
            set { place = value; }
        }

        public void ShowList()
        {
            for (int i = 0; i < myList.Count; i++)
            {
                Console.WriteLine("" + myList[i] + "\n|");
            }
        }

        public void AddToList()
        {          
            myList.Add("Name: " + name + " Car: " + car + " Place: " + place); 

        }

    }

Here is how I am trying to save myList to a text file:

private void button3_Click(object sender, EventArgs e)
        {
            using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"C:\Users\Keil\Documents\Visual Studio 2013\Projects\CarRacingArrList\racers.txt"))
            {
                file.WriteLine(person.myList.ToString());
            }

        }

I added the ToString() method to cast all the elements inside myList to a string.

The output I get into my textfile is this: System.Collections.ArrayList

Is the the result of invalid casting or just something I have missed out?

Thankyou

Recommended Answers

All 5 Replies

person.myList.ToString()
Here myList is an object of type ArrayList. The ToString of it will print out what it knows, namely : System.Collections.ArrayList
Will try to rearrange your code, will let you know.

Thankyou very much

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

namespace ConsoleApplication3
{
    class Person
    {
        // shortcut syntax that does exactly the same as you did
        // the C# compiler is smart enough to fill in the missing details
        public string Name { get; set; }
        public string Car { get; set; }
        public string Place { get; set; }
    }

    //your code
    class People
    {
        private string name;
        private string car;
        private string place;
        public ArrayList myList = new ArrayList();

        public string Name
        {
            get { return name; }
            set { name = value; }
        }

        public string Car
        {
            get { return car; }
            set { car = value; }
        }

        public string Place
        {
            get { return place; }
            set { place = value; }
        }

        public void ShowList()
        {
            for (int i = 0; i < myList.Count; i++)
            {
                Console.WriteLine("" + myList[i] + "\n|");
            }
        }

        public void AddToList()
        {          
            myList.Add("Name: " + name + " Car: " + car + " Place: " + place); 

        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            List<Person> racers = new List<Person>();
            //here a form of object initialistion is used
            Person racer1 = new Person { Name = "John", Car = "Porche", Place = "Belgium" };
            racers.Add(racer1);
            Person racer2 = new Person { Name = "Betty", Car = "BMW", Place = "France" };
            racers.Add(racer2);
            //etc
            //print out list or write to file
            foreach (Person P in racers)
            {
                Console.WriteLine("Name: " + P.Name + " Car: " + P.Car + " Place: " + P.Place);
            }
            Console.ReadKey();
        }
    }
}

Have fun! Any questions please ask.

Thankyou for your help. With the auto implemented properties do we not need to specify the 'private' accessor?

Auto implemented properties are just a handy shortcut for plain
vanilla properties. At least I use them that way.
Just set and get a Color as example.
For anything more fancy I still use the normal syntax.
You might also read this article.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.