Note that there are some explanatory texts on larger screens.

plurals
  1. POC# What is the best way to determine the type of an inherited interface class?
    text
    copied!<p>In my application I work with criterias. I have one base Criteria interface and and other interfaces who inherits from this base interface:</p> <pre> ICriteria | | ---------------------- | | ITextCriteria IChoices </pre> <p>What I'd like to know is, what is the best way to know what Type the class is?</p> <p>In my code I have a dropdown box and based on that I have to determine the type:</p> <pre><code>// Get selected criteria var selectedCriteria = cmbType.SelectedItem as ICriteria; if (selectedCriteria is IChoices) { //selectedCriteria = cmbType.SelectedItem as IChoices; Doesn't work IChoices criteria = selectedCriteria as IChoices;//cmbType.SelectedItem as IChoices; SaveMultipleChoiceValues(criteria); //_category.AddCriteria(criteria); } else { //ICriteria criteria = selectedCriteria; //cmbType.SelectedItem as ICriteria; if (selectedCriteria.GetCriteriaType() == CriteriaTypes.None) { return; } //_category.AddCriteria(criteria); } _category.AddCriteria(selectedCriteria); selectedCriteria.LabelText = txtLabeltext.Text; this.Close(); </code></pre> <p>My question is, is this the best way? Or is there a better way to achieve this? </p> <p>The chance is big that there are coming more interfaces based on ICriteria.</p> <p>EDIT:</p> <p>I have 2 types of controls which I want to add dynamically to my application. One control is a textbox and the other is a radio button. </p> <p>For a radio button the user can define the options. When the options are defined, the user must choose one of the options and the chosen option must be saved in the database (this is later used to perform search operations). So, when the Save button is clicked, I have to determine the chosen type (radio or text) and save the answer possibilities (if it is a radio). </p> <p>For a textbox, this doesn't have any answer possibilities. For that reason it has a different interface.</p> <p>I hope I make it a little bit clearer now. Here is another question which is related: <a href="https://stackoverflow.com/questions/4526011/c-how-to-implement-interface-where-concrete-classes-differs">C# How to implement interface where concrete classes differs?</a></p> <p>EDIT II:</p> <p>This is how my method <code>SaveMultipleChoiceValues</code> looks like:</p> <pre><code>private void SaveMultipleChoiceValues(IChoices criteria) { foreach (DataGridViewRow row in dgvCriteriaControls.Rows) { if (row == dgvCriteriaControls.Rows[dgvCriteriaControls.Rows.Count - 1]) continue; //multipleChoice.AddChoice(row.Cells["Name"].Value.ToString()); string choice = row.Cells["Name"].Value.ToString(); criteria.AddChoice(choice); } } </code></pre>
 

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