Cast Timer To Component

  • Thread starter Thread starter Albert Krüger
  • Start date Start date
A

Albert Krüger

Hello,

I want to cast a System.ComponentModel.Component to a
System.Windows.Forms.Timer. In the help of the .net framework I read that
System.Windows.Forms.Timer inherits from the
System.ComponentModel.Component. But always I get a runtime error when I try
to cast.
Where is my mistake?

objTimer = DirectCast(objComponent, System, System.Windows.Forms.Timer)
'System.InvalidCastException

Thanks
Albert Krüger
 
Sorry, but I think it is the same problem because System.Timers.Timer
inherits also from System.ComponentModel.Component.

Albert
 
That is not my day. I don't think. Now I have forgotten to change the
DirectCast-Type.

Albert
 
Albert Krüger said:
Sorry, but I think it is the same problem because System.Timers.Timer
inherits also from System.ComponentModel.Component.

Check the type before performing the cast:

\\\
Dim o As Component = ...
If TypeOf o Is System.Windows.Forms.Timer Then
Dim t As System.Windows.Forms.Timer = _
DirectCast(o, System.Windows.Forms.Timer)
...
End If
///
 
I don't read this VB dialect, however I recognize an 'InvalidCastException'
when I see one.

to give you a clue try:
Console.WriteLine(objComponent.GetType().Name)
 
Back
Top