ActiveX dll - could someone explain what is happening?
I'm trying ActiveX in my project, but I'm still confused.
My ActiveX object is CMyX, and I use that object from my Client app.
In one method I have declared 2 ActiveX objects.
Then I add some data to my new created objects and I run some action.
Code:
'in a Method of a client application
Dim obj1 as New CMyX, obj2 as New CMyX
obj1.AddData( Item1 )
obj2.AddData( Item2 )
obj1.RunSomeAction()
I supposse I have 2 separate objects.
Surprisse, my Obj1 contains all data: Item1 and Item2.
Could somebody help me understand what is happening?
Re: ActiveX dll - could someone explain what is happening?
Not sure I follow you ergas.
I tried reprocducing your error in my small project, and I didn't get the same.
What does your AddData and RunSomeAction look like ¿
I am attaching my project(s) here, so that you can see ( for what it's worth in any case )
1 Attachment(s)
Re: ActiveX dll - could someone explain what is happening?
Hallo Great Hannes
Thank's for your example.
I have added a module with a public index variable and a public array, so it is now simmilar to my project.
With a public array, if you add data to both Objects, you have all your data in one Object.
It seems like both ActiveX objects are sharing public module space, only classes are private.
ergas
Re: ActiveX dll - could someone explain what is happening?
Hello again ergas! :wave:
A Quick question, if you don't mind...
Don't you think having a function / sub with an "array parameter" might work better here, meaning, that function / sub 's purpose would just be to add all the elements from the array, meaning we don't have to reference the dll's array, but just pass array objects / elements to it. What do you think ¿
I'm busy with it, and I'll see what I can come up with..
Re: ActiveX dll - could someone explain what is happening?
Hi Great Hannes
The purpose why I'm experimenting with ActiveX is to convert my old exe project to ActiveX. It's full of global variables and arrays. I can't change that, it would be to complicated.
I'm considerring a possibility to convert this project to ActiveX and to use it's main function through an ActiveX public interface. My hope was, ActiveX object shall isolate data from the rest of my program.
That's why I'm trying this examples. I want to build an ActiveX, make a public interface and have all variables and public arrays isolated from my client application. I want to be able to declare several objects at the same time.
But it seems it doesn't work.
Maybe should I make an ActiveX exe project, and play with instancing property?
Re: ActiveX dll - could someone explain what is happening?
Quote:
Originally Posted by ergas
Hallo Great Hannes
With a public array, if you add data to both Objects, you have all your data in one Object.
It seems like both ActiveX objects are sharing public module space, only classes are private.
ergas
The Problem is in your understanding of a class and ovbviously no knowledge about what a modul does when having a class and a module.
Modules are Common for all classes of one dll so its a bit tricky to use moduls when you want to create a class especially in your case as you are using public variables so they are global in your class.
If you really need to use moduls in a classdefinition I would suggest to define all variables in your class as private and this way encapsuling them using properties.In your modul if necessary you may then use this properties to access all the data. Thats the full sense of having data encapsulated but you didn't there was not single one private variable nothing is encapsulated so why wonderng that this doesn't work. You are using the same array all the time
Re: ActiveX dll - could someone explain what is happening?
Yes Jonny ( The OOP Guru ) is correct. You'd have to make use of 2 separate arrays. Object 1 will refer to & use Array 1; Object 2 will refer to & use Array 2.
Re: ActiveX dll - could someone explain what is happening?
Quote:
Originally Posted by HanneSThEGreaT
Yes Jonny ( The OOP Guru ) is correct. You'd have to make use of 2 separate arrays. Object 1 will refer to & use Array 1; Object 2 will refer to & use Array 2.
The best way would be to have one array only as nobody knows how much object of this type of class he will have but simple having the array itself as a part of his class.
He can use a module only for data which are common to all members of this class, which is basically totally against any class design. He can have arrays, collections, other classes whatever as prrivate members of his class but nothing in a module. Modules are for methods only in that case.
and in that case using members of the class by their public properies only.
Quote:
Originally Posted by ergas
I'm considerring a possibility to convert this project to ActiveX and to use it's main function through an ActiveX public interface. My hope was, ActiveX object shall isolate data from the rest of my program.
Changing some code from non OOP to OOP isn't that easy as it seems on the first short look,it can be done, but it cannot be done using classes as a gate to global variables, because globals stay globals even when they are packed into dlls
your code
Code:
Option Explicit
Public ItemStr As String
Public Sub AddData(ItemInfo As String)
ItemInfo = ItemInfo & " Test 1"
ItemStr = ItemInfo
End Sub
'add to array and increment index
Public Sub Add2Array(value As Integer)
arr(Index) = value
Index = Index + 1
End Sub
Public Sub RunSomeAction()
MsgBox ItemStr & " " & arr(0) & " " & arr(1) & " " & arr(2) & " " & arr(3) & " " & arr(4)
End Sub
Public Sub InitArray()
Index = 0
ReDim arr(10)
End Sub
'---------------- and in the bas module you have ----------
Public arr() As Integer
Public index As Integer
This is totally contraproductive to all what we learn about classes, encapsulation and all that. you simple need
Code:
Option Explicit
Private m_arr() As Integer
Private m_index As Integer
Private m_itemStr As String
Public Sub AddData(itemInfo As String)
itemInfo = itemInfo & " Test 1"
m_iItemStr = itemInfo
End Sub
'add to array and increment index
Public Sub Add2Array(value As Integer)
m_arr(Index) = value
m_index = m_index + 1
End Sub
Public Sub RunSomeAction()
MsgBox m_itemStr & " " & m_arr(0) & " " & m_arr(1) & " " & m_arr(2) & " " & m_arr(3) & " " & m_arr(4)
End Sub
Private Sub Class_Initialize()
m_index = 0
ReDim m_arr(10) ' I dont know why you dont dim it with 10 jut from the beginning
End Sub
This will really encapsulate your variables so you cannot access ItemStr from outside the class now.
Quote:
Originally Posted by ergas
It seems like both ActiveX objects are sharing public module space, only classes are private.
yes and no. It is not of interest if you put your globls into a dll or not. globals stay globals. If you are using classes, you may or may not use them in a dll or in an activeX.exe thats all the same as it is loaded to your program and works as you design it. If you want encapsulaton yo can use a class a usercontrol a form and the data in that sheet (of the class, of the Usercontrol of the form... If you are using modules and declare public variables they are ( in VB ) global !!
BTW: If a program which is not OOP Style gets to complicated, believe me: 100 rds of hours unnecessary work to change it. Take a new start and a new design and recreate it in OOP style. But - better in the same way change to vb.net or C# 2005 because its much easier there to work with real OOP Style ;) AND FORGET TO only think about the lost hours for the old program. Dont misspend your time in only trying it. You will change to classes and find you on a point where you would have needed the encapsulated values to be not encapsulated and other points, where you would have needed them to be separated because of a new instance to show. You will get totally confused some point later, believe me. I have gne this way some years ago. Its the way into the wood !
Re: ActiveX dll - could someone explain what is happening?
Great answer Jonny! :thumb: