Link to home
Start Free TrialLog in
Avatar of sierrahalflife
sierrahalflife

asked on

Class modules

I have a long running problem with them.
I can't Dim WithEvents, becuase i get "Object Variable Or With block variable not set" when i try to use it
but I use Events, so I need the WithEvents.

Here's the start of a class module I'm working on:

'local variable(s) to hold property value(s)
Private mvarServer As String 'local copy
Private mvarPort As Long 'local copy
Private mvarNick As String 'local copy
'local variable(s) to hold property value(s)
Private mvarWinSockHost As Variant 'local copy
'To fire this event, use RaiseEvent with the following syntax:
'RaiseEvent OnMsg[(arg1, arg2, ... , argn)]
Public Event OnMsg(Nick As String, Channel As String, Message As String)

Public Sub CTCPreply(Target As String, Message As String, Optional Arguments As String)
End Sub

Public Sub PrivMsg_(Target As String, Message As String)
End Sub

Public Sub CTCP(Target As String, Message As String, Optional Arguments As String)
End Sub

Public Property Set WinSockHost(ByVal vData As Variant)
'used when assigning an Object to the property, on the left side of a Set statement.
'Syntax: Set x.WinSockHost = Variant1
    Set mvarWinSockHost = vData
End Property


Public Property Get WinSockHost() As Variant
'used when retrieving value of a property, on the right side of an assignment.
'Syntax: Debug.Print X.WinSockHost
    Set WinSockHost = mvarWinSockHost
End Property



Public Sub PrivMsg(Message As String, Target As String)
End Sub

Public Sub Connect()
End Sub

Public Property Let Nick(ByVal vData As String)
'used when assigning a value to the property, on the left side of an assignment.
'Syntax: X.Nick = 5
    mvarNick = vData
End Property


Public Property Get Nick() As String
'used when retrieving value of a property, on the right side of an assignment.
'Syntax: Debug.Print X.Nick
    Nick = mvarNick
End Property



Public Property Let Port(ByVal vData As Long)
'used when assigning a value to the property, on the left side of an assignment.
'Syntax: X.Port = 5
    mvarPort = vData
End Property


Public Property Get Port() As Long
'used when retrieving value of a property, on the right side of an assignment.
'Syntax: Debug.Print X.Port
    Port = mvarPort
End Property



Public Property Let Server(ByVal vData As String)
'used when assigning a value to the property, on the left side of an assignment.
'Syntax: X.Server = 5
    mvarServer = vData
End Property


Public Property Get Server() As String
'used when retrieving value of a property, on the right side of an assignment.
'Syntax: Debug.Print X.Server
    Server = mvarServer
End Property

Ok... I used the builder...
This is how i'm calling it:

Dim WithEvents objIRC As SHL_IRC.IRC

Private Sub Form_Load()
objIRC.Connect
End Sub

IRC gurus: I know that there isn't enough info provided to connect... It's just the framework at the moment...
Avatar of supunr
supunr

you get "Object Variable Or With block variable not set" error because you have not yet allocated memory.  SO before you use the object, just allocate memory as showen below.

Private WithEvents SomeObject as ObjectType

Private Sub SOmeFunction()
    if (SomeObject Is Nothing) then
        ' Allocate memory to the object
        Set SomeObject = New ObjectType
    End If
    ' Now you are free to use the object
End Sub

Hopefully this work and this help.

Good Luck!
ASKER CERTIFIED SOLUTION
Avatar of Arthur_Wood
Arthur_Wood
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of sierrahalflife

ASKER

Ah! thanks!
I though i needed a New in it somewhere
but i was putting it in the WithEvents line :\
unfortunately, you can't declare an Object WITHEVENTS, AND NEW in the same declaration.

AW
I noticed ;)