Vb.net 如何将Timer.Enabled转换为type控件

Vb.net 如何将Timer.Enabled转换为type控件,vb.net,vb6,Vb.net,Vb6,将代码从VB6转换到VB.NET后,我得到了以下结果代码: 设计器表单代码: Public WithEvents Timer1 As Microsoft.VisualBasic.Compatibility.VB6.TimerArray Me.Timer1 = New Microsoft.VisualBasic.Compatibility.VB6.TimerArray(components) Private Function GetBaseControl(ByRef a_type As Stri

将代码从VB6转换到VB.NET后,我得到了以下结果代码:

设计器表单代码:

Public WithEvents Timer1 As Microsoft.VisualBasic.Compatibility.VB6.TimerArray
Me.Timer1 = New Microsoft.VisualBasic.Compatibility.VB6.TimerArray(components)
Private Function GetBaseControl(ByRef a_type As String) As System.Windows.Forms.Control Implements GetBaseControl

Select Case a_type

Case "Web"
        GetBaseControl = ctBrowser1(0)
Case "Timer"
        GetBaseControl = Timer1(0)

End Select
End Function
代码实现:

Public WithEvents Timer1 As Microsoft.VisualBasic.Compatibility.VB6.TimerArray
Me.Timer1 = New Microsoft.VisualBasic.Compatibility.VB6.TimerArray(components)
Private Function GetBaseControl(ByRef a_type As String) As System.Windows.Forms.Control Implements GetBaseControl

Select Case a_type

Case "Web"
        GetBaseControl = ctBrowser1(0)
Case "Timer"
        GetBaseControl = Timer1(0)

End Select
End Function
这里是我收到的错误:

无法在第
GetBaseControl=Timer1(0)
行将“Boolean”类型的值转换为“System.Windows.Forms.Control”


这在VB6中运行良好

如果这在VB6中起作用,那么代码会遵循一些非常糟糕的做法,返回布尔值
Enabled
,而不是实际的
计时器
组件。这意味着,至少,选择严格是关闭的,这真的很糟糕

在这种情况下,.Net中的
计时器不再被视为控件。相反,它们是
组件
。您需要重新思考此代码的功能。代码的使用示例可能会让我们推荐一种不同(更好)的方法来解决这个问题

在这种情况下,我怀疑重新考虑使用重载方法(这不是vb6的惯用方法)会产生更好的结果,特别是在保持类型安全性方面,而不是在传递字符串方面



注意:在第二天编辑问题之前,这个答案更有意义

,即使我删除了仍然有错误的答案:类型为“System.Windows.Forms.Timer”的值无法转换为“System.Windows.Forms.Control”。啊,没错。。。计时器是组件,而不是控件。即使它们不是,您仍然在使用兼容性名称空间。您会发现.Net非常特别,因为您知道所使用的每个对象的精确类型,而vb6/vbscript则不那么挑剔。随着时间的推移,你会习惯这一点,并将其视为一种优势,但当你第一次学习.Net时,它可能会成为阻碍。有更好的解决方案吗?如何解决此错误?