Returning multiple values

I have a function that identifies coordinates on a page and I return them as

Dictionary<int, Collection<Rectangle>> GetDocumentCoordinates(int DocumentId)

      

However, later on I need information about each page - if it was checked, what is the page resolution, color / bw, etc. I could create another function and execute almost the same result set as the previous function and get this information.

Dictionary<int, PageInfo> GetDocumentAttributes(int DocumentId)

      

Another alternative would be to add a parameter ref

so that I can return these values.

Dictionary<int, Collection<Rectangle>> GetCoordinates(int DocumentId, ref Dictionary<int, PageInfo> PageAttributes)

      

Another alternative is to create a class containing the dictionary and page information:

class DocumentInfo
{
    Dictionary<int, Collection<Rectangle>> Coordinates { get; set;}
    Dictionary<int, PageInfo> PageAttributes { get; set; }
}

      

and then define:

DocumentInfo GetDocumentInfo(int DocumentId);

      

I'm leaning towards the latter, but your ideas are greatly appreciated.

+1


source to share


2 answers


The latter option is by far the best. I have found that when accepting or returning complex data with multiple values, creating a complex type to encapsulate that data is best practice for a number of reasons.

First, your return data will likely change as your design changes. Encapsulating this data into an object allows you to change what it carries and how your methods work with that data without changing the interfaces of your objects. Obviously, your data object doesn't need to implement the interface; at most, have a base class with minimal interface, and then pass references to the base around.



Second, you may find that your data becomes complex to the point where you need to perform validation. Instead of having this check in all the methods of your classes that you act on this data, you can easily wrap it in a data class. Single responsibility, etc.

+6


source


You seem to need a lot of data. The latter should be accurate and extensible; you wanted (to make it easier to use Dictionary<,>

) you could encapsulate things a little more, but the fact that C # does not directly support named indexed properties means you will need multiple classes, unless you wrap with methods like

class DocumentInfo {
    Dictionary<int, Collection<Rectangle>> rectangles = ...
    public Collection<Rectangle> GetRectangles(int index) {
        return rectangles[index]; // might want to clone to
                                  // protect against mutation
    }
    Dictionary<int, PageInfo> pages = ...
    public PageInfo GetPageInfo(int index) {
        return pages[index];
    }

 }

      



I don't quite understand what it is int

, so I can't tell how reasonable it is (so I just left it alone).

Also - with the first option you probably won't need it ref

- that's enough to use out

.

+1


source







All Articles