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 :)

1
  • 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()? Commented Apr 5, 2016 at 8:24

3 Answers 3

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);
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

... or override ToString in text (OP should chose another name which is conform to .NET capitalization conventions like Text).
@TimSchmelter thanks, i had a argumentation about overriding ToString yesterday so i didnt want to mention it first stackoverflow.com/questions/36398000/…
however, instead of ToString and reflection i'd prefer this approach for that task.
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.

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.