instantiate a class and set the properties

A

Aidy

Firstly thanks Jonathan for your reply re: class modules.

This is my current problem.

I have got the below code in a VBA class module

<snip>
Option Explicit
Private mWaitForString As String
Private mStringToSend As String

Private WithEvents m_ttHost As teemtalk.Host
Public Sub WaitAndSend()

Set m_ttHost = CurrentSession.Host
With m_ttHost
.ClearWaitFors
.WaitFor mWaitForString
End With
End Sub

Private Sub m_ttHost_OnWaitforString(ByVal WaitFor As String)
If (WaitFor = mWaitForString) Then
m_ttHost.Send mStringToSend
End If
End Sub
Public Property Let WaitForString(sWaitforString As String)
mWaitForString = sWaitforString
End Property

Public Property Let StringToSend(sStringToSend As String)
mStringToSend = sStringToSend
End Property
<snip>

Now, I want to instantiate this class and set the properties in
a normal module

This is where I am

<snip>
Public obj As WaitForClass
Sub test()

Set obj = New WaitForClass

obj.WaitAndSend

End Sub
<snip>

Thank You

Aidy
 
J

Jonathan West

Hi Adiy,

There's nothing obviously wrong with that code. Have you set a reference to
the teemtalk and CurrentSession objects in Project References?
 
A

Aidy

Jonathan West says:
There's nothing obviously wrong with that code. Have you set a reference to
the teemtalk and CurrentSession objects in Project References?

Jonathan,

I have now stabilised what I am doing. What I am unsure of is how to
set the properties of a class during instantiation.

This is what I am doing at the moment.
obj.WaitForString = ("Password:")
obj.StringToSend = ("NICOLA1")
obj.WaitAndSend

Where 'WaitForString' and 'StringToSend' are properties and
'WaitAndSend' is the method. Is this the correct way of doing it?

Thanks

Aidy
 
M

Martin Seelhofer

Hi Aidy
[...] Is this the correct way of doing it?

Basically, the answer is *yes*. However, there are a few things
you might improve in your code:

1. you don't have to use brackets around your property values,
the following code works perfectly well:

obj.WaitForString = "Password:"
obj.StringToSend = "NICOLA1"

2. you might - for readability's sake - want to emphasize the
difference between property assignments and method calls by
using the Call-keyword for method calls:

Call obj.WaitAndSend()


Cheers,
Martin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top