I'm trying to iterate a list of forms (Mdichildren), and compare values of the specific forms in the list. However using var to encompass any type of Form makes it difficult to use form.Value that may be specific to a given form.
foreach (var oven in Ovens) // list of objects
{
foreach (var form in this.Mdichildren) // contains multiple form types
{
if (form is Form_OvenControl)
{
if (oven.Id == form.ovenId)
{
// this fails because form.ovenId is treated like var, not like Form_OvenControl
}
}
else if (form is Form_Instructions)
{
// do something with different type of form
}
}
}
I know I could cast the value and make a new variable of that type of form, but I want to use the object by reference in the list. I'm sure I could figure out a hacky way but I'm sure there's a neat way to do something (less illegal, but) like:
(Form_OvenControl)form.formSpecificValue = 0;
EDIT: as was kindly pointed out below, I can simply do the following:
if (form is Form_OvenControl)
{
var ovenform = form as Form_OvenControl;
if (oven.Id == ovenform.ovenId)
{
}
}
And the cast form will still refer to the item in the list I'm looking to alter.
I had just thought there was a way to use form as Form_OvenControl and access a variable within it essentially in one line.