|
-
Feb 25th, 2006, 05:09 AM
#1
Thread Starter
Member
Error with WithEvents...
Im using WithEvents to use a winsock object in my class
(uses as following: Dim WithEvents wskConnection as Winsock )
Now in my class initialize method, I use the following code:
wskConnection.Close
and I get an error 91...
Here is a code snippet:
VB Code:
Option Explicit
Dim WithEvents wskYMSG As Winsock
Public iUsers As Integer '# of users in the room
Public iPrivates As Integer '# of private messages received
Private Sub Class_Initialize()
sStatus = "offline"
iUsers = 0
iPrivates = 0
wskYMSG.Close
End Sub
When I create a new object, and the class initializes, I get an error 91, I click Debut, and the line wskYMSG.Close is highlited..
-
Feb 25th, 2006, 06:21 AM
#2
PowerPoster
Re: Error with WithEvents...
Dim WithEvents wskYMSG As Winsock
You need to change it to
and then you have to use WithEvents on its own
-
Feb 25th, 2006, 07:26 AM
#3
Re: Error with WithEvents...
You have only declared your winsock object, but you haven't created it. You should keep the declaration as it is (even though I would have preferred to use either the Private or Public keyword instead of simply Dim). But you'll need to create it before you use any of the properties or methods of the object.
VB Code:
Private Sub Class_Initialize()
sStatus = "offline"
iUsers = 0
iPrivates = 0
[b] Set wskYMSG = New Winsock [/b]
wskYMSG.Close
End Sub
However I don't really understand why you would need to Close the Winsock object in the Initialize event since you haven't opened it yet.
-
Feb 25th, 2006, 02:42 PM
#4
Thread Starter
Member
Re: Error with WithEvents...
When I use
Set wskYMSG = New Winsock
I get an error saying "Invalid use of New keyword"... (and winsock isnt in the list of object that appears when I type New)...
-
Feb 25th, 2006, 02:50 PM
#5
Re: Error with WithEvents...
try the Set statement under Form_Load event.
VB Code:
Private Sub Form_Load()
Set wskYMSG = New Winsock
End Sub
Harsh Gupta
-
Feb 25th, 2006, 02:51 PM
#6
Thread Starter
Member
Re: Error with WithEvents...
I get an error!... the object is declared in the class, not in the form!
(Although New Winsock still isnt an available choice, even in the form...I wonder why)
-
Feb 25th, 2006, 02:59 PM
#7
Re: Error with WithEvents...
oh oh.. sorry, didnot notice that it was class ini. function.
ok, one more try, use:
VB Code:
Public wskYMSG As Winsock
'instead of Dim WithEvents wskYMSG As Winsock
and in the form, in which you are creating an object of this class, under Load event, use the above Set statement.
(Although New Winsock still isnt an available choice, even in the form...I wonder why)
the above method will make New winsock available in Intellisense.
but i am confused. why are you closing the winsock in th class ini. function?? it means that you are not using the object at all. (just curious to know)
Harsh Gupta
-
Feb 25th, 2006, 03:03 PM
#8
Thread Starter
Member
Re: Error with WithEvents...
LOL Ok actually I was just closing the winsock in the initialize event so I can see if it was working easily.... at first the wskYMSG.close was in another method, and I got the error, so I decided to place it in the initialize event since it was easier to test....
it wont stay there
-
Feb 25th, 2006, 03:06 PM
#9
Thread Starter
Member
Re: Error with WithEvents...
mmmm well Im pretty sure it will work if I declare it with
"Public wskYMSG as Winsock"
the problem is, I need with events!...since I have to code some of the winsock events in my class!...and you Cant use "New" with WithEvents
-
Feb 25th, 2006, 03:12 PM
#10
Re: Error with WithEvents...
ok, so if you want to use withevents, try Public WithEvents bla.. instead of Dim WithEvents bla.., and use the set statement under class ini. function.
Harsh Gupta
-
Feb 25th, 2006, 03:15 PM
#11
Thread Starter
Member
Re: Error with WithEvents...
That's what I tried when I got
"Invalid use of New keyword" error
-
Feb 25th, 2006, 03:19 PM
#12
Thread Starter
Member
Re: Error with WithEvents...
VB Code:
Option Explicit
Public WithEvents wskYMSG As Winsock
Public iUsers As Integer '# of users in the room
Public iPrivates As Integer '# of private messages received
Public sUsername As String
Public sPassword As String
Public sStatus As String
Private Sub Class_Initialize()
Set wskYMSG = New Winsock
sStatus = "offline"
iUsers = 0
iPrivates = 0
wskYMSG.Close
End Sub
this is the code...and it wont work..(Invalid use of new keyword)
-
Feb 26th, 2006, 12:34 AM
#13
Thread Starter
Member
Re: Error with WithEvents...
mmmm still cant get it to work....
is there any other way to use a winsock control inside a class?
-
Feb 26th, 2006, 09:08 AM
#14
Re: Error with WithEvents...
Do you have a Winsock component or are you trying to use the Winsock control in that manner? That won't work, the Winsock control must reside on a Form or UserControl. Try downloading a Winsock class instead. There is a good one at VBIP.com.
-
Feb 26th, 2006, 05:46 PM
#15
Re: Error with WithEvents...
You can create a winsock control dynamically by adding a reference to MSWINSCK.OCX
Goto Project\References Browse to system32\MSWINSOCK.OCX
Note: don't add the Winsock component to the toolbox or this will not work.
Once the reference has been added declare like so
VB Code:
Private WithEvents Winsock As MSWinsockLib.Winsock
Set Winsock = New MSWinsockLib.Winsock
-
Feb 26th, 2006, 08:31 PM
#16
Member
Re: Error with WithEvents...
Create a new ActiveX EXE project. Put in this code:
Dim frm As Form
Dim WithEvents wsk As Winsock
Private Sub Class_Initialize()
Set frm = New Form1
Load frm
Set wsk = frm.Winsock1
End Sub
Private Sub Class_Terminate()
Unload frm
Set frm = Nothing
wsk.Close
Set wsk = Nothing
End Sub
Add to the project a form (form1). Place on it the winsock control (you will need to go to the components tab and add it first).
Now in you class you can reference wsk (the winsock control) and you will see you can respond to its events ie.
Private Sub wsk_ConnectionRequest(ByVal requestID As Long)
End Sub
Private Sub wsk_Error(ByVal Number As Integer, Description As String, ByVal Scode As Long, ByVal Source As String, ByVal HelpFile As String, ByVal HelpContext As Long, CancelDisplay As Boolean)
End Sub
-
Feb 26th, 2006, 11:09 PM
#17
Thread Starter
Member
Re: Error with WithEvents...
Thanks everyone! I was able to do it by adding the Winsock as a reference instead of a component!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|