|
-
Dec 1st, 2002, 09:19 AM
#1
Thread Starter
New Member
Simple AcitveX DLL QUESTION (I hope)
In this code:
Code:
Option Explicit
' PROPERTIES':
Private m_comm As MSComm
Private m_num As Long
Private m_str As String
' METHODS:
Public Sub function1()
m_str = "Str1"
m_num = 1
End Sub
Public Sub function2(ByVal var As String)
m_str = "Str2"
m_num = 2
m_comm.CommPort = 1 <-------!!!! ERROR!!!!!
End Sub
Error: "Object variable or With block variable not set (Error 91)"
Why I can't assign the value ?????
Thanks
-
Dec 1st, 2002, 03:14 PM
#2
m_comm is an object and must be assigned to something before it can be used. Somewhere in your class you need to assign an instance of the MSComm control to your m_comm variable.
Typically a property Get/Set is used
VB Code:
Public Property Get Communications() as MSCommLib.MSComm
Set Communications = m_comm
End Property
Public Property Set Communications(CommControl as MSCommLib.MSComm
Set m_comm = CommControl
End Property
Private Sub Class_Terminate()
Set m_comm = nothing
End Sub
-
Dec 2nd, 2002, 07:02 PM
#3
Thread Starter
New Member
Thank you brucevde for the answer.
But I don't understood this:
GET/SET Properties are used to communicate with the class; they are used to READ a private member variable and to WRITE a private member varable from outside the class (i.e: from the exe application that "call" the class).
But in my case I'm WRITING the private member variable from within the class => I don't understand the error.
I hope u'll reply me.
MatteoXMad
-
Dec 2nd, 2002, 07:56 PM
#4
MSComm is a control and must be put on a form same as a textbox, label etc.. You cannot create a new instance of the MSComm control within your class - you need a form. This means you need some method of telling your class which control you want it to use, thus the Property Get/Set.
This is no different than your exe application calling your class. Let's say your class is named clsCommPort and you code these lines.
Dim objCommPort as clsCommPort
objCommPort.Function1
You will receive the error 91 in your exe because you have not created an instance of the class object. You must add the line Set objCommPort = New clsCommPort.
-
Dec 3rd, 2002, 12:57 PM
#5
Thread Starter
New Member
After your answer I tried many solutions, without a correct result.
PROJECT ActiveX DLL named Class1:
VB Code:
Option Explicit
' PROPERTIES':
Private m_comm As MSComm
Private m_num As Long
Private m_str As String
Public Sub function1(ByVal var As String)
MsgBox var
m_str = "Str1"
m_num = 1
m_comm.CommPort = 1 '<-------!!!! ERROR!!!!!
End Sub
Public Property Get Communications() As MSCommLib.MSComm
Set Communications = m_comm
End Property
Public Property Set Communications(CommControl As MSCommLib.MSComm)
Set m_comm = CommControl
End Property
PROJECT Standard EXE (that call Class1):
VB Code:
Private Sub Command1_Click()
Dim class As New Class1
class.function1 ("pippo")
End Sub
An error occur immediatly with GET/SET Properties...
:-(
Help me!
MatteoXMad
-
Dec 4th, 2002, 05:28 PM
#6
Since you are creating a DLL you are probably getting the error "Private objects cannot be used in Public..." , the rules are different for ActiveX DLLs. You need to declare your Property Get/Set as the generic Object and then test to make sure the client is passing a control of the correct type.
PROJECT ActiveX DLL named Class1:
-- Note that this project must reference the Microsoft MSComm Control component.
VB Code:
Option Explicit
' PROPERTIES':
Private m_comm As MSComm
Private m_num As Long
Private m_str As String
Public Sub function1(ByVal var As String)
MsgBox var
m_str = "Str1"
m_num = 1
m_comm.CommPort = 1
End Sub
Public Property Get Communications() As Object '<---Changed to As Object
Set Communications = m_comm
End Property
Public Property Set Communications(CommControl As Object) '<---Changed to As Object
If Typeof CommControl Is MSCommLib.MSComm Then '<---Check if the class is passed the proper cotnrol
Set m_comm = CommControl
Else
Err.Raise -1,,"Requires a Microsoft Comm Control" '<--Raise an error to the client app
End if
End Property
PROJECT Standard EXE (that call Class1): - You still have not told the Class which instance of the Comm control it should use.
VB Code:
Private Sub Command1_Click()
Dim class As New Class1
Set class.Communications = Form1.MSComm1 '<--- Tell the class to use the Comm control on Form1.
class.function1 ("pippo")
End Sub
-
Dec 5th, 2002, 03:58 PM
#7
Thread Starter
New Member
Thank u very much for your replies. I'll try as soon as possible.
MatteoXMad
-
Dec 6th, 2002, 01:36 PM
#8
Thread Starter
New Member
Code:
Thank you very much for your help, the code you gave me works.
You are GREAT, really!
So this is the CONCLUSION:
If an EXE Application wants to call the function connect() of the ActiveX DLL and this function use some components
(i.e. MSComm) => also the EXE App. MUST reference those components!!!
------------------
------------ connect() | |
| |---------->| ActiveX DLL |
| EXE App. | | |
| |<----------| MSComm |
------------ | | |
------|-----------
|
|----> serial line
There's no way to do this without using references in the EXE App.????
Thanks again
MatteoXMad
-
Dec 6th, 2002, 10:18 PM
#9
Never say never. I probably should have mentioned this before but there is a way to have the ActiveX DLL handle everything. This hides the fact that you are using the MSComm control from the EXE application. You could easily upgrade the DLL with a different control in the future without affecting the EXE.
Add a Form to your DLL project that contains an MSComm control, my example below names it frmCommunications.
When a new instance of your Class is created load an instance of the form (it doesn't have to be visible).
Your class could use its instance of the form to call the control directly.
The Get/Set properties in the first example can be removed from the class.
You might have problems if two programs are using the DLL at the same time.
VB Code:
'Private m_comm As MSComm '<---No longer required
Private m_num As Long
Private m_str As String
Private frmMyComm as frmCommunications
Public Sub function1(ByVal var As String)
MsgBox var
m_str = "Str1"
m_num = 1
frmMyComm.MSComm1.CommPort = 1 '<--Call the control directly
End Sub
Private Sub Class_Initialize
Set frmMyComm = New frmCommunications '<--initialize the class instance of the form
Load frmMyComm
End Sub
Private Sub Class_Terminate
Unload frmMyComm '<-- make sure everything is cleaned up
Set frmMyComm = Nothing
End Sub
Standard EXE (that call Class1) does not need to reference the MSComm control and can simply be called by this function (hey that's what you had originally).
VB Code:
Private Sub Command1_Click()
Dim class As New Class1
class.function1 ("pippo")
End Sub
Hope this gives you more ideas.
-
Dec 11th, 2002, 04:55 PM
#10
Thread Starter
New Member
Thank u again for the answer. I'll try to use the code u give me as soon as possible. It looks like that it'll work.
So we've understood that to use objects inside a class, we must include these in a form.
Do u know c++? It works different, really?
Bye
Mad
-
Dec 11th, 2002, 08:13 PM
#11
So we've understood that to use objects inside a class we must include these in a form
That should be "to use controls inside a class we must include these in a form." Any COM object can be used in a class.
I am familiar with C++ but my career took me down the VB path. I would have trouble writing any windows program in C nowadays. Its been so long (6+ years).
-
Dec 15th, 2002, 05:21 PM
#12
Thread Starter
New Member
Why? Objects or controls are so different?
Maybe, when we speak about controls, we are referring to ActiveX and COM architecture?
Bye
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
|