I've written some apps using simple imperative way - now I want to learn more about building software the object oriented way.

My goal is to take an Excel file and load one of the tables into memory. I can do this by:

  1. Creating a list of strings for each row, and store the table in list
  2. Creating a structure/ class for the records, and store the table in list of objects

The simplest is of course first solution, but I want to know the advantages of second - most importantly which one is better and when?

From my perspective creating an struct/object for each record, and then using fields/properties to get the values makes the program unnecessarily complicated (if I plan to load the table to database even more). For some reason though this was one of the proposed solution this question: Are 2 dimensional Lists possible in c#?.

  • You can refer this rule: msdn.microsoft.com/en-us/library/ms229017.aspx – xqMogvKW Dec 10 '16 at 9:22
  • I assume this is a C# question? – CodesInChaos Dec 10 '16 at 9:33
  • @CodesInChaos maybe, I don't know how much other languages differ. I mean it in general, when developing something using OOP – Mikołaj Chełpa Dec 10 '16 at 9:35
  • In C++ the difference is only the default visibility of members. Java doesn't support user-defined structs at all. C#/VB.NET and Delphi are pretty similar in that regard. – CodesInChaos Dec 10 '16 at 9:43
  • 2
    When developing something using OOP, you should use objects, because if you don't use objects, you are not developing something using OOP. However, e.g. in C♯, VB.NET, C++, instances of structs are objects, so for those particular languages, your question doesn't make sense, because if you are using structs, you are using objects. – Jörg W Mittag Dec 10 '16 at 9:44

I guess you are talking about one two-dimensional table in an Excel sheet, where each column has a certain meaning, not a full excel document with all possible meta information.

If you know the specific columns and their meaning beforehand, at compile time, (typically the columns have headers with some header titles), then using a class or struct with specific fields is in most cases the better approach. That way, you make the column structure explicit in your program's code, each time you access a value from a certain column you access a named field, which makes it much more understable what your program is doing right there. Additionally, your program becomes more type safe, which means less error-prone.

However, if you are going to write a program which deals with arbitrarily structured tables, where the columns and headers and the meaning of the columns is not known at compile time, then you need a more generic data structure like a two-dimensional list of strings.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.