J
J
Jaguar_sea2016-02-15 05:45:21
OOP
Jaguar_sea, 2016-02-15 05:45:21

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));
            }   
      }
}

I would like the output class to provide an immutable collection. Help, please, to define the interface of such class.
Of course, the very principle of creating classes is interesting, i.e. step by step thought process. If you share a link to the material describing this approach, I will be very grateful.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Saboteur, 2016-02-15
@saboteur_kiev

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 question

Ask a Question

731 491 924 answers to any question