Answer the question
In order to leave comments, you need to log in
How to learn to create good classes?
I, like all beginners, tried to go from simple to complex. From creating a console application that displays some data on the screen, to a class that would display this data in a convenient form and at the same time expose only the method that the user needs.
My console application that parses the xml file with SMS messages from the bank and displays the balance on a certain date looks like this:
XmlDocument XmlDoc = new XmlDocument();
XmlDoc.Load("sms.xml");
XmlElement xRoot = XmlDoc.DocumentElement;
foreach (XmlNode xNode in xRoot)
{
if (xNode.Attributes.Count > 0)
{
XmlNode attr = xNode.Attributes.GetNamedItem("address");
if (attr.Value == "bank")
{
// Методы GetDateFromDateField и GetBalanceFromBodyField, с помощью регулярных
// выражений парсят исходные данные.
Console.Write(GetDateFromDateField(xNode.Attributes.GetNamedItem("date").Value) + " ");
Console.WriteLine(GetBalanceFromBodyField(xNode.Attributes.GetNamedItem("body").Value));
}
}
}
Answer the question
In order to leave comments, you need to log in
A class is not a function to provide output.
A class is a collection of data and methods that operate on that data.
Start from this definition - that the class should contain the data you need, and you need to work with this data using the methods declared inside this class.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question