473,396 Members | 2,017 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

How to cast using a Type object?

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

Nov 16 '05 #1
10 16336
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

"Dave Leach" <ba***@agilent.com> wrote in message
news:8C**********************************@microsof t.com...
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 Mode
{
OFF,
CW,
TEST,
ACTIVE,
CAL
};

public enum Mode_MUI
{
OFF = Mode.OFF,
CW = Mode.CW,
ALIVE = Mode.ACTIVE,
B_TEST = Mode.TEST,
D_CAL = Mode.CAL,
};

public enum Mode_RUI
{
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,
Dave


Nov 16 '05 #2
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/de...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.

Nov 16 '05 #3
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
Nov 16 '05 #4
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

Nov 16 '05 #5
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

Nov 16 '05 #6
>> 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.

Perhaps you can post your MyComboBox class or email it to me
(mg*@Atrevido.net).
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

Nov 16 '05 #7
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.
Nov 16 '05 #8
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/quickst...oc/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/de...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.

Nov 16 '05 #9
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.

Nov 16 '05 #10
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.

Nov 16 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Dave Byron | last post by:
I am having trouble with DBNull's from my SQL server. I am building/converting a asset web app from Access and my db has nulls on various fields . I have tried searching newsgroups and tried to get...
1
by: Svyatoslav | last post by:
Hi, I have a problem with XmlNodes and my stack. It looks something like this: //declarations XmlNode node, new_node; Stack MyStack = new Stack(); //code MyStack.Push(node);
8
by: Steve Wasser | last post by:
I'm pulling SQL data into a dataset to be used to perform some math against. I asked this question earlier, but the answer someone gave me left me with further questions. The SQL data is stored as...
2
by: Dave Leach | last post by:
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...
4
by: Kimmo Laine | last post by:
Hi, how can i cast to ref object, in C#: internal void Foo( ref object o ) { // . . . } // . . .
3
by: Karel Vandenhove | last post by:
Hi, I get an error "cannot apply indexing with to an expression of type object" when I try to compile the code below. SSLScannerManager is a COM component. (Used to access fingerprint...
1
by: Chad Silva | last post by:
Hi all I have an object created via reflection of type CateringObject.FBO. I'm trying to assign it to a property that accepts objects of type CateringObject.IDeliveryDestination (which is...
3
by: cv | last post by:
How to cast a System.Object type to a class instance? For example, Form1 has a Listbox. I create a custom class MyClass and add several instances of that class to the Listbox. When the user...
9
by: Ben | last post by:
Hello, I'm not a developper, so sorry if it's a stupid question... I'm trying to develop an application in vb.net and I have the following problem: I have some information in an array:...
5
by: AMDRIT | last post by:
I would like to cast an object to a value type specified by a variable of Type Function ReturnTest(InputVar as Object) as Object Dim DataType as Type = GetType(System.String) If TypeOf...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.