Problem with Classes and Modules 
Author Message
 Problem with Classes and Modules

Hi,

I have created an ActiveX which has a class called clsMyClass and a module
called modMyModule. In modMyModule, there's a global variable called
lngMyProp and declared as: "Public lngMyProp As Long"
And in clsMyClass a property (MyProp) uses lngMyProp as its local variables:

Public Property Get MyProp() As Long
    MyProp = lngMyProp
End Property

Public Property Let MyProp(ByVal NewValue As Long)
    lngMyProp = NewValue
End Property

My problem is that when I create two instances of my ActiveX and set MyProp
property of  the first object, the MyProp property of the second one is also
set.

Dim mycls1 As New clsMyClass
Dim mycls2 As New clsMyClass

mycls1.MyProp = 123456
MsgBox mycls2.MyProp    'That displays "123456" <= ??????????

What can I do?



Sat, 18 Sep 2004 14:32:28 GMT  
 Problem with Classes and Modules


Quote:
> Hi,

> I have created an ActiveX which has a class called clsMyClass and a module
> called modMyModule. In modMyModule, there's a global variable called
> lngMyProp and declared as: "Public lngMyProp As Long"
> And in clsMyClass a property (MyProp) uses lngMyProp as its local

variables:
[SNIP]

Quote:
> My problem is that when I create two instances of my ActiveX and set
MyProp
> property of  the first object, the MyProp property of the second one is
also
> set.
[SNIP]
> What can I do?

Easy.  Don't use global variables to store local values.


Sat, 18 Sep 2004 16:08:58 GMT  
 Problem with Classes and Modules
try using late binding


Quote:
> Hi,

> I have created an ActiveX which has a class called clsMyClass and a module
> called modMyModule. In modMyModule, there's a global variable called
> lngMyProp and declared as: "Public lngMyProp As Long"
> And in clsMyClass a property (MyProp) uses lngMyProp as its local
variables:

> Public Property Get MyProp() As Long
>     MyProp = lngMyProp
> End Property

> Public Property Let MyProp(ByVal NewValue As Long)
>     lngMyProp = NewValue
> End Property

> My problem is that when I create two instances of my ActiveX and set
MyProp
> property of  the first object, the MyProp property of the second one is
also
> set.

> Dim mycls1 As New clsMyClass
> Dim mycls2 As New clsMyClass

> mycls1.MyProp = 123456
> MsgBox mycls2.MyProp    'That displays "123456" <= ??????????

> What can I do?



Sun, 19 Sep 2004 03:36:59 GMT  
 Problem with Classes and Modules
Hi,

I need to do this, because I have another classes which use this variable.

Soheil


Quote:



> > Hi,

> > I have created an ActiveX which has a class called clsMyClass and a
module
> > called modMyModule. In modMyModule, there's a global variable called
> > lngMyProp and declared as: "Public lngMyProp As Long"
> > And in clsMyClass a property (MyProp) uses lngMyProp as its local
> variables:
> [SNIP]
> > My problem is that when I create two instances of my ActiveX and set
> MyProp
> > property of  the first object, the MyProp property of the second one is
> also
> > set.
> [SNIP]
> > What can I do?

> Easy.  Don't use global variables to store local values.



Sun, 19 Sep 2004 04:59:17 GMT  
 Problem with Classes and Modules
How?


Quote:
> try using late binding



> > Hi,

> > I have created an ActiveX which has a class called clsMyClass and a
module
> > called modMyModule. In modMyModule, there's a global variable called
> > lngMyProp and declared as: "Public lngMyProp As Long"
> > And in clsMyClass a property (MyProp) uses lngMyProp as its local
> variables:

> > Public Property Get MyProp() As Long
> >     MyProp = lngMyProp
> > End Property

> > Public Property Let MyProp(ByVal NewValue As Long)
> >     lngMyProp = NewValue
> > End Property

> > My problem is that when I create two instances of my ActiveX and set
> MyProp
> > property of  the first object, the MyProp property of the second one is
> also
> > set.

> > Dim mycls1 As New clsMyClass
> > Dim mycls2 As New clsMyClass

> > mycls1.MyProp = 123456
> > MsgBox mycls2.MyProp    'That displays "123456" <= ??????????

> > What can I do?



Sun, 19 Sep 2004 04:58:22 GMT  
 Problem with Classes and Modules


Quote:
> Hi,

> I need to do this, because I have another classes which use this variable.

If you use a global variable to store a local value, then you will see the
result that you are seeing.  If you don't want such results, don't use a
global variable.  End of discussion.

Now if you have other classes which need to refer to a particular variable,
then you will need to approach that differently.  Look at your overall
approach to the issue.  You have not provided sufficient information for any
further assistance in this matter.

Basically, global variables are global, by definition.  What you are seeing
is expected.  If it is not what you want, then you will have to do something
different.



Sun, 19 Sep 2004 11:16:23 GMT  
 Problem with Classes and Modules


Quote:
> try using late binding

Why?


Sun, 19 Sep 2004 11:17:07 GMT  
 Problem with Classes and Modules
This problem sounds like a threading issue ...

Quote:
>-----Original Message-----
>Hi,

>I have created an ActiveX which has a class called

clsMyClass and a module
Quote:
>called modMyModule. In modMyModule, there's a global
variable called
>lngMyProp and declared as: "Public lngMyProp As Long"
>And in clsMyClass a property (MyProp) uses lngMyProp as

its local variables:
Quote:

>Public Property Get MyProp() As Long
>    MyProp = lngMyProp
>End Property

>Public Property Let MyProp(ByVal NewValue As Long)
>    lngMyProp = NewValue
>End Property

>My problem is that when I create two instances of my

ActiveX and set MyProp
Quote:
>property of  the first object, the MyProp property of the
second one is also
>set.

>Dim mycls1 As New clsMyClass
>Dim mycls2 As New clsMyClass

>mycls1.MyProp = 123456
>MsgBox mycls2.MyProp    'That displays "123456"
<= ??????????

>What can I do?

>.



Tue, 12 Oct 2004 06:40:08 GMT  
 Problem with Classes and Modules
This is not a threading issue but rather the default behaviour of global
namespace.
The public variables you declare in a regular module will be shared by all
instances of the class.
You should instead declare the variable as private in the class module.
If you have procedures in the BAS module that need to handle this value you
should let the class pass them as an argument to these procedures.

Best regards

Joacim Andersson


Quote:
> This problem sounds like a threading issue ...

> >-----Original Message-----
> >Hi,

> >I have created an ActiveX which has a class called
> clsMyClass and a module
> >called modMyModule. In modMyModule, there's a global
> variable called
> >lngMyProp and declared as: "Public lngMyProp As Long"
> >And in clsMyClass a property (MyProp) uses lngMyProp as
> its local variables:

> >Public Property Get MyProp() As Long
> >    MyProp = lngMyProp
> >End Property

> >Public Property Let MyProp(ByVal NewValue As Long)
> >    lngMyProp = NewValue
> >End Property

> >My problem is that when I create two instances of my
> ActiveX and set MyProp
> >property of  the first object, the MyProp property of the
> second one is also
> >set.

> >Dim mycls1 As New clsMyClass
> >Dim mycls2 As New clsMyClass

> >mycls1.MyProp = 123456
> >MsgBox mycls2.MyProp    'That displays "123456"
> <= ??????????

> >What can I do?

> >.



Tue, 12 Oct 2004 17:34:24 GMT  
 
 [ 9 post ] 

 Relevant Pages 

1. Class modules 97 - Refering to array within a custom class module

2. Accessing procedures of a class module from another class module

3. Circular Reference Between Modules when 2 Class Modules Listen to Each others Events

4. Class Module and Module

5. Class Modules & Modules

6. VB Modules/Class Modules

7. class module vs. module

8. modules and class modules

9. Returning the name of a module/class module

10. Modules or Class modules?

11. Q(VB5) : Different between module & class module

12. Q: Code Module vs UserControl (Form, Class) module.

 

 
Powered by phpBB® Forum Software