1

I haven't made a custom class before so this might not be possible, I want to read a number of text files and store certain information about them to be used throughout my program.

class text
    {
        public int IDnum { get; set; }
        public string file { get; set; }
        public int lineNum { get; set; }
        public string FileText { get; set; }
        public string lineType { get; set; }
    }

    List<text> listOne = new List<text>();
    internal void ReadFile()
    {
        try
        {
            int IDtype = 0;
            foreach (string x in resultFiles)
            {
                using (StreamReader sr = new StreamReader(x))
                {
                    string s;//text line

                    int LINECOUNT = 0;
                    string type = "not defined";
                    while ((s = sr.ReadLine()) != null)// this reads the line
                    {
                        if(s.Contains("COMPUTERNAME="))
                        {
                            type = "PC Name";
                        }

                        if (s.Contains("Original Install Date:     "))
                        {
                            type = "Original Install Date";
                        }
                        if (LINECOUNT==2)
                        {
                            type = "other Date";
                        }
                        if (s.Contains("DisplayName\"="))
                        {
                                type = "Add/Remove Programs";
                        }

                        text text1 = new text { IDnum = IDtype,  lineNum=LINECOUNT, file=x, FileText=s, lineType=type}; 
                        LINECOUNT++;
                        IDtype++;
                        listOne.Add(text1);
                    }

                    sr.Close();
                }
            }
            foreach(var x in listOne)
            {
                MessageBox.Show(x.ToString());
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString());
        }
    }

However when I try to read the list it just returns the same value "names of program.name of class.text"

I have never built custom classes before can anyone point me to a website where I can learn more examples?

Thanks in advance for any advice :)

Chong Ching
  • 425
  • 3
  • 7
  • 2
    You need to override the `.ToString()`-Method in your text-class to get a proper result when calling `x.ToString()`. What result do you expect when calling ToString()? – Camo Apr 05 '16 at 08:24

3 Answers3

3

x.ToString() doesn't work because it's a type of your class and not string.

you can access the properties of the item

foreach (var x in listOne)
{
    MessageBox.Show(x.file + " " + x.FileText);
}

or override the ToString() method in your class - then you can use x.ToString()

class text
{
    public int IDnum { get; set; }
    public string file { get; set; }
    public int lineNum { get; set; }
    public string FileText { get; set; }
    public string lineType { get; set; }

    public override string ToString()
    {
        return string.Format("{0}, {1}", this.file, this.FileText);
    }
}
fubo
  • 44,811
  • 17
  • 103
  • 137
  • 2
    ... or override `ToString` in `text` (OP should chose another name which is conform to .NET capitalization conventions like `Text`). – Tim Schmelter Apr 05 '16 at 08:26
  • @TimSchmelter thanks, i had a argumentation about overriding ToString yesterday so i didnt want to mention it first http://stackoverflow.com/questions/36398000/create-comma-separated-list-from-list-of-objects/36398086#36398086 – fubo Apr 05 '16 at 08:32
  • however, instead of `ToString` and reflection i'd prefer [this](http://stackoverflow.com/a/36421337/284240) approach for that task. – Tim Schmelter Apr 05 '16 at 08:48
1

listOne is list of the class text, so in the loop you actually print the class name and not the content. You can print the content by calling the members you defined

foreach(var x in listOne)
{
     MessageBox.Show(x.IDnum + " " + x.file + ...);
}

As a side note, class names in C# should start with capital letter.

Guy
  • 46,488
  • 10
  • 44
  • 88
1

To use the ToString() method you have to override it e.g. in the following way:

class text
{
    public int IDnum { get; set; }
    public string file { get; set; }
    public int lineNum { get; set; }
    public string FileText { get; set; }
    public string lineType { get; set; }

    public override ToString()
    {
        return fileText; // return here whatever you want to use
    }
}

You use the ToString() to get information about your Test class instances. You will get a better result, if you implement the ToString like above, or use a property of the class.

scher
  • 1,813
  • 2
  • 18
  • 39