VB程序员博客

VB程序开发

我在类模块中使用winsock的connect就出现了错误
定义
Private WithEvents Send_Http As Winsock
出错的地方
Send_Http.Connect "127.0.0.1", 80

怎么回事呢?!

建议定义变量不要含有下划线

Private WithEvents s As Timer

Private Sub Class_Initialize()
s.Interval = 100
s.Enabled = True
End Sub

Private Sub s_Timer()
Debug.Print Now
End Sub

这样也会出现错误,是不是类模块中不能使用其他组件哦?!

上面的是class1中的

下面的代码是form1中的
Dim n As Class1

Private Sub Form_Load()
Set n = New Class1
End Sub

引用 3 楼 fisheep_works 的回复:
Private WithEvents s As Timer

Private Sub Class_Initialize()
s.Interval = 100
s.Enabled = True
End Sub

Private Sub s_Timer()
Debug.Print Now
End Sub

这样也会出现错误,是不是类模块中不能使用其他组件哦?!


当然出错了,timer控件没有实例化,可以用settime函数代替timer控件

引用 3 楼 fisheep_works 的回复:
Private WithEvents s As Timer

Private Sub Class_Initialize()
s.Interval = 100
s.Enabled = True
End Sub

Private Sub s_Timer()
Debug.Print Now
End Sub

这样也会出现错误,是不是类模块中不能使用其他组件哦?!


在类模块当中这样子定义s不会自动实例化.由于ActiveX控件的特殊性,不方便这样子使用.建议使用Active Object代替或者最好使用API.


标签: , , ,