How to cast using a Type object?

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
 
M

Michael Giagnocavo [MVP]

What type is c.SelectedItem? Because that's what matters. If it's an object,
then there's no point casting it first.

But anyways, System.ComponentModel.TypeConverter can do what you want.

-mike
MVP
 
J

Jeffrey Tan[MSFT]

Hi Dave,

I agree with mike that you should use TypeConverter, for more information
about how to implement TypeConverter, please refer to:

"Implementing a Type Converter"
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/htm
l/cpconimplementingtypeconverter.asp

Thank you for your patience and cooperation. If you have any questions or
concerns, please feel free to post it in the group. I am standing by to be
of assistance.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
M

Michael Giagnocavo [MVP]

foreach ( MyComboBox c in cbControls )
{
queryArgs.Index = c.Index;
SettingReqEvents.RaiseSettingReqEvent( this, queryArgs );
c.SelectedItem = ( CAST )queryArgs.Dat;
}


What type is queryArgs.Dat?

-mike
MVP
 
G

Guest

Mike

As stated in the posting, the type of the data contained in the object Dat will be of various enumeration types. The instance of the derived MyComboBox knows which enumeration type to expect, but the Dat object may contain a value of different but related type. I need to cast the Dat object to the expected type so that the assignment to the ComboBox SelectedIndex property matches the control's items

Sigh, if only C# supported templates. I'd simply instantiate an instance of MyComboBox control with the expected type

Anyway, any suggestion on how to accomplish the cast would be appreciated

Thanks
Dav

----- Michael Giagnocavo [MVP] wrote: ----

foreach ( MyComboBox c in cbControls

queryArgs.Index = c.Index
SettingReqEvents.RaiseSettingReqEvent( this, queryArgs )
c.SelectedItem = ( CAST )queryArgs.Dat


What type is queryArgs.Dat

-mik
MV
 
G

Guest

Mike

Sorry, but I mistyped a key word in the reply copied below. The assignment is to the ComboBox SelectedItem property, not the SelectedIndex property

Dav

----- Dave Leach wrote: ----

Mike

As stated in the posting, the type of the data contained in the object Dat will be of various enumeration types. The instance of the derived MyComboBox knows which enumeration type to expect, but the Dat object may contain a value of different but related type. I need to cast the Dat object to the expected type so that the assignment to the ComboBox SelectedIndex property matches the control's items

Sigh, if only C# supported templates. I'd simply instantiate an instance of MyComboBox control with the expected type

Anyway, any suggestion on how to accomplish the cast would be appreciated

Thanks
Dav

----- Michael Giagnocavo [MVP] wrote: ----

foreach ( MyComboBox c in cbControls

queryArgs.Index = c.Index
SettingReqEvents.RaiseSettingReqEvent( this, queryArgs )
c.SelectedItem = ( CAST )queryArgs.Dat


What type is queryArgs.Dat

-mik
MV
 
M

Michael Giagnocavo [MVP]

As stated in the posting, the type of the data contained in the object
Perhaps you can post your MyComboBox class or email it to me
([email protected]).
Sigh, if only C# supported templates. I'd simply instantiate an instance
of MyComboBox control with the expected type!

Well, you're getting generics in v2 :).

-mike
MVP
 
J

Jeffrey Tan[MSFT]

Hi Dave,

Thanks for your feedback.

I will spend some time on this issue, I will reply to you ASAP.

Thanks for your understanding.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
J

Jeffrey Tan[MSFT]

Hi Dave,

Sorry for letting you wait for so long time.

Based on my understanding, you just want cast your child class to its
"actually" type, so that you can invoke certain method or field of that
type.

I think you can use another approach to get this done.

You may use reflection to dynamically invoke certain type's methods and
fields.

To invoke certain method, you may use 2 approaches:
1. using Type.InvokeMember method
2. using Type.GetMethod method, then use MethodInfo.Invoke method

For more information, please refer to:
http://samples.gotdotnet.com/quickstart/howto/doc/Invoke.aspx

To invoke certain property or field, also use Type.InvokeMember with
BindingFlags GetField, SetField , GetProperty ,SetProperty etc. For more
information, please refer to:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/
frlrfsystemreflectionbindingflagsclasstopic.asp

===================================
Please apply my suggestion above and let me know if it helps resolve your
problem.

Thank you for your patience and cooperation. If you have any questions or
concerns, please feel free to post it in the group. I am standing by to be
of assistance.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
J

Jeffrey Tan[MSFT]

Hi babel,

Does my workaround of using Reflection make sense to you? Do you still have
any concern on this issue?

Please feel free to post. Thanks

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
J

Jeffrey Tan[MSFT]

Hi Dave,

Thanks very much for your feedback.

I am glad you got what you want. If you need further help, please feel free
to post.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top