Results 1 to 17 of 17

Thread: Error with WithEvents...

  1. #1

    Thread Starter
    Member
    Join Date
    Feb 2006
    Posts
    32

    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:
    1. Option Explicit
    2. Dim WithEvents wskYMSG As Winsock
    3. Public iUsers As Integer '# of users in the room
    4. Public iPrivates As Integer '# of private messages received
    5.  
    6. Private Sub Class_Initialize()
    7.     sStatus = "offline"
    8.     iUsers = 0
    9.     iPrivates = 0
    10.  
    11.     wskYMSG.Close
    12.    
    13. 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..

  2. #2
    PowerPoster
    Join Date
    Apr 2005
    Location
    Debug.Print
    Posts
    3,885

    Re: Error with WithEvents...

    Dim WithEvents wskYMSG As Winsock
    You need to change it to
    VB Code:
    1. Dim wskYMSG As Winsock
    and then you have to use WithEvents on its own

  3. #3
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    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:
    1. Private Sub Class_Initialize()
    2.     sStatus = "offline"
    3.     iUsers = 0
    4.     iPrivates = 0
    5. [b]    Set wskYMSG = New Winsock [/b]
    6.     wskYMSG.Close
    7. 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.

  4. #4

    Thread Starter
    Member
    Join Date
    Feb 2006
    Posts
    32

    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)...

  5. #5
    Smitten by reality Harsh Gupta's Avatar
    Join Date
    Feb 2005
    Posts
    2,938

    Re: Error with WithEvents...

    try the Set statement under Form_Load event.
    VB Code:
    1. Private Sub Form_Load()
    2.     Set wskYMSG = New Winsock
    3. End Sub
    Harsh Gupta
    Show Appreciation. Rate Posts.

  6. #6

    Thread Starter
    Member
    Join Date
    Feb 2006
    Posts
    32

    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)

  7. #7
    Smitten by reality Harsh Gupta's Avatar
    Join Date
    Feb 2005
    Posts
    2,938

    Re: Error with WithEvents...

    oh oh.. sorry, didnot notice that it was class ini. function.

    ok, one more try, use:
    VB Code:
    1. Public wskYMSG As Winsock
    2. '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
    Show Appreciation. Rate Posts.

  8. #8

    Thread Starter
    Member
    Join Date
    Feb 2006
    Posts
    32

    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

  9. #9

    Thread Starter
    Member
    Join Date
    Feb 2006
    Posts
    32

    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

  10. #10
    Smitten by reality Harsh Gupta's Avatar
    Join Date
    Feb 2005
    Posts
    2,938

    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
    Show Appreciation. Rate Posts.

  11. #11

    Thread Starter
    Member
    Join Date
    Feb 2006
    Posts
    32

    Re: Error with WithEvents...

    That's what I tried when I got
    "Invalid use of New keyword" error

  12. #12

    Thread Starter
    Member
    Join Date
    Feb 2006
    Posts
    32

    Re: Error with WithEvents...

    VB Code:
    1. Option Explicit
    2. Public WithEvents wskYMSG As Winsock
    3. Public iUsers As Integer '# of users in the room
    4. Public iPrivates As Integer '# of private messages received
    5. Public sUsername As String
    6. Public sPassword As String
    7. Public sStatus As String
    8.  
    9. Private Sub Class_Initialize()
    10.     Set wskYMSG = New Winsock
    11.    
    12.     sStatus = "offline"
    13.     iUsers = 0
    14.     iPrivates = 0
    15.    
    16.    
    17.     wskYMSG.Close
    18.    
    19. End Sub


    this is the code...and it wont work..(Invalid use of new keyword)

  13. #13

    Thread Starter
    Member
    Join Date
    Feb 2006
    Posts
    32

    Re: Error with WithEvents...

    mmmm still cant get it to work....

    is there any other way to use a winsock control inside a class?

  14. #14
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    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.

  15. #15
    Old Member moeur's Avatar
    Join Date
    Nov 2004
    Location
    Wait'n for Free Stuff
    Posts
    2,712

    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:
    1. Private WithEvents Winsock As MSWinsockLib.Winsock
    2. Set Winsock = New MSWinsockLib.Winsock

  16. #16
    Member
    Join Date
    Jan 2006
    Posts
    33

    Smile 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

  17. #17

    Thread Starter
    Member
    Join Date
    Feb 2006
    Posts
    32

    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
  •  



Click Here to Expand Forum to Full Width