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 :)
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);
}
}
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.
To use the ToString()
method you have to override it eg 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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.