Note that there are some explanatory texts on larger screens.

plurals
  1. POWhat is the ideal way to call an overloaded method based on an object that may be of one of several different types?
    primarykey
    data
    text
    <p>For my WPF view, I have several objects that implement a single interface for display purposes. When one is selected, I want to call a certain method based on the type and options chosen on the view. I already have Overloads available for each type of object. I attempted using a generic method, but it proved that the objects were too different to be useable.</p> <p>I am trying to avoid control coupling with large if statements for maintainability purposed. </p> <p><strong>Did I go wrong by using the singular interface, or is there an interesting maintainable way of doing this?</strong></p> <p>I have a datagrid that contains a collection of IDisplayableObject.</p> <pre><code>public interface IDisplayableObject { string Name { get; set; } string ID { get; set; } } </code></pre> <p>Right now, I cast as the first answer.</p> <pre><code> public void ExportDashed(string path, IDisplayableObject folder) { if (folder is TestFolder) { IndentationTraverse(folder as TestFolder); } else if (folder is UserStory) { IndentationTraverse(folder as UserStory); } _excelManipulator.Save(path); } </code></pre> <p>I want to avoid the if statements. For this case, I usually use a dictionary mapped to an Action, but I also have another method of </p> <pre><code> public void ExportShiftingColumns(string path, IDisplayableObject folder) { if (folder is TestFolder) { ColumnShiftTraverse(folder as TestFolder); } else if (folder is UserStory) { ColumnShiftTraverse(folder as UserStory); } _excelManipulator.Save(path); } </code></pre> <p>So, I need to make the action, Export Dashed or Export Shifting Columns, then I need to decide which type to export.</p> <p><strong>You can see from the code, duplication is not a good thing and I want to avoid it as much as possible. For this application, it is not that big of a deal but more for my personal learning.</strong></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload