G
Guest
I am writing a Windows MDI application in C#. I would like to cast an object to a specific type. I know that the object is of a compatible type. The code below shows what I would like to do, but does not compile, where the cast "(c.ItemType)" produces the error "type or namespace could not be found". I know that each control "c" has a public property "ItemType" which returns a Type
foreach ( MyComboBox c in cbControls
name = (string)controlToNamesTable[ c ]
this.queryArgs.Index = (Int32)allNamesToIndexTable[ name ]
SettingReqEvents.RaiseSettingReqEvent( this, queryArgs )
c.SelectedItem = (c.ItemType)queryArgs.Dat
Basically what I am trying to do is cycle through a collection of MyComboBox controls, obtain the current setting value by raising an event. The event argument returned contains an object Dat which contains the current setting. The setting is of an enumeration type, but the enumeration type is overloaded. I need to cast it to the type expected by the MyComboBox control when setting the SelectedItem property. One example enumeration type set is shown below
public enum Mod
OFF
CW
TEST
ACTIVE
CA
}
public enum Mode_MU
OFF = Mode.OFF
CW = Mode.CW
ALIVE = Mode.ACTIVE
B_TEST = Mode.TEST
D_CAL = Mode.CAL
}
public enum Mode_RU
OFF = Mode.OFF
CW = Mode.CW
CALL = Mode.ACTIVE
CELL = Mode.ACTIVE
GTest = Mode.TEST
GCal= Mode.CAL
}
The "Dat" object will contain a value of either Mode_MUI or Mode_RUI type, which I should be able to cast to type Mode_MUI. Note that each control in the collection of MyComboBox controls will be of a different enumeration type, similar to the example provided below. So I want to be able to cast the "Dat" object to the type known by the control (ItemType)
Is there a way to use the control's ItemType member to cast the object to the correct type
Thanks
Dav
foreach ( MyComboBox c in cbControls
name = (string)controlToNamesTable[ c ]
this.queryArgs.Index = (Int32)allNamesToIndexTable[ name ]
SettingReqEvents.RaiseSettingReqEvent( this, queryArgs )
c.SelectedItem = (c.ItemType)queryArgs.Dat
Basically what I am trying to do is cycle through a collection of MyComboBox controls, obtain the current setting value by raising an event. The event argument returned contains an object Dat which contains the current setting. The setting is of an enumeration type, but the enumeration type is overloaded. I need to cast it to the type expected by the MyComboBox control when setting the SelectedItem property. One example enumeration type set is shown below
public enum Mod
OFF
CW
TEST
ACTIVE
CA
}
public enum Mode_MU
OFF = Mode.OFF
CW = Mode.CW
ALIVE = Mode.ACTIVE
B_TEST = Mode.TEST
D_CAL = Mode.CAL
}
public enum Mode_RU
OFF = Mode.OFF
CW = Mode.CW
CALL = Mode.ACTIVE
CELL = Mode.ACTIVE
GTest = Mode.TEST
GCal= Mode.CAL
}
The "Dat" object will contain a value of either Mode_MUI or Mode_RUI type, which I should be able to cast to type Mode_MUI. Note that each control in the collection of MyComboBox controls will be of a different enumeration type, similar to the example provided below. So I want to be able to cast the "Dat" object to the type known by the control (ItemType)
Is there a way to use the control's ItemType member to cast the object to the correct type
Thanks
Dav