Static class membervariables? 
Author Message
 Static class membervariables?

Hi,

Within C++ it is possible to declare static membervariables in a class.
The effect is that its value is shared among all instances of the class.
This feature can for example be used to give each instance of the class
an unique ID. Just declare a static (private) long within the class, and
increment it each time an instance of the class is created.

I need this (an unique id for each instance of the class) in a VB class.
Is this possible?

Thanks for any suggestions,

Fred Bakker



Mon, 23 Apr 2001 03:00:00 GMT  
 Static class membervariables?
Not really. A workaround is to compile your class in an ActiveX
DLL, and use a global, module-level variable as your counter.
You can do this without making a DLL, but in a DLL, the global
counter is encapsulated better.

--
Ron Ruble
Raffles Software Development, Inc.

Patient - Doctor, it hurts when I do this!
Doctor - Then don't do that.

Quote:

>Hi,

>Within C++ it is possible to declare static membervariables in a class.
>The effect is that its value is shared among all instances of the class.
>This feature can for example be used to give each instance of the class
>an unique ID. Just declare a static (private) long within the class, and
>increment it each time an instance of the class is created.

>I need this (an unique id for each instance of the class) in a VB class.
>Is this possible?

>Thanks for any suggestions,

>Fred Bakker



Mon, 23 Apr 2001 03:00:00 GMT  
 Static class membervariables?
This also won't work if you create objects of class using different clients.
VB's appartment threading duplicates data for every apartment (MSDN).
I posted same question. But it lloks like it's not possible using basic lang
only. So u need to go for API to achieve that.

Prasad

Quote:

>Not really. A workaround is to compile your class in an ActiveX
>DLL, and use a global, module-level variable as your counter.
>You can do this without making a DLL, but in a DLL, the global
>counter is encapsulated better.

>--
>Ron Ruble
>Raffles Software Development, Inc.

>Patient - Doctor, it hurts when I do this!
>Doctor - Then don't do that.


>>Hi,

>>Within C++ it is possible to declare static membervariables in a class.
>>The effect is that its value is shared among all instances of the class.
>>This feature can for example be used to give each instance of the class
>>an unique ID. Just declare a static (private) long within the class, and
>>increment it each time an instance of the class is created.

>>I need this (an unique id for each instance of the class) in a VB class.
>>Is this possible?

>>Thanks for any suggestions,

>>Fred Bakker



Tue, 24 Apr 2001 03:00:00 GMT  
 Static class membervariables?
@cts.com
X-MimeOLE: Produced By Microsoft MimeOLE V5.00.0810.800
Newsgroups: microsoft.public.vb.enterprise,microsoft.public.vb.syntax,comp.lang.basic.visual.misc

The follow is a copy from the MSDN library. It is the best method i've seen
so far.

Greywolf Ghost

---Begin---
Class Modules vs. Standard Modules

---Text Removed for Clarity---

Static Class Data
There may be occasions when you want a single data item to be shared among
all objects created from a class module. This is sometimes referred to as
static class data.

You cannot implement true static class data in a Visual Basic class module.
However, you can simulate it by using Property procedures to set and return
the value of a Public data member in a standard module, as in the following
code fragment:

' Read-only property returning the application name.
Property Get CommonString() As String
   ' The variable gstrVisibleEverywhere is stored in a
   ' standard module, and declared Public.
   CommonString = gstrVisibleEverywhere
End Property

Note   You cannot use the Static keyword for module-level variables in a
class module. The Static keyword can only be used within procedures.

It's possible to simulate static class data that's not read-only by
providing a corresponding Property Let procedure or Property Set for a
property that contains an object reference to assign a new value to the
standard module data member. Using global variables in this fashion violates
the concept of encapsulation, however, and is not recommended.

For example, the variable gstrVisibleEverywhere can be set from anywhere in
your project, even from code that doesn't belong to the class that has the
CommonString property. This can lead to subtle errors in your program.

---End---


Quote:
>Hi,

>Within C++ it is possible to declare static membervariables in a class.
>The effect is that its value is shared among all instances of the class.
>This feature can for example be used to give each instance of the class
>an unique ID. Just declare a static (private) long within the class, and
>increment it each time an instance of the class is created.

>I need this (an unique id for each instance of the class) in a VB class.
>Is this possible?

>Thanks for any suggestions,

>Fred Bakker



Thu, 26 Apr 2001 03:00:00 GMT  
 Static class membervariables?
If running in MTS, you can create a shared property.

Joel

Quote:

>Hi,

>Within C++ it is possible to declare static membervariables in a class.
>The effect is that its value is shared among all instances of the class.
>This feature can for example be used to give each instance of the class
>an unique ID. Just declare a static (private) long within the class, and
>increment it each time an instance of the class is created.

>I need this (an unique id for each instance of the class) in a VB class.
>Is this possible?

>Thanks for any suggestions,

>Fred Bakker



Fri, 27 Apr 2001 03:00:00 GMT  
 Static class membervariables?
The best solution for what you want, based on the specifics you provided, is
to use ObjPtr().  This will return you the pointer address of the VB
instance.  Since no two instance can exist at the same address, it is
guaranteed to be unique.

If the number you need should be some sort of incremented value, you'll have
to use one of the other methods provided.

You should note however that objects already contain a unique identity.  The
use of IDs is not that common with object development.  While your domain
might indicate a unique ID like "Social Security Number" on an object, your
code probably should avoid using this ID to identify objects...



Sun, 29 Apr 2001 03:00:00 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. Static class membervariables?

2. Static class membervariables?

3. static classes and methods

4. Static Class and Variable

5. creating static classes?

6. Static class data

7. Static class module in VBA

8. Static class attributes, how to create them.

9. Class Side (Static) functions

10. C++ static class property possible in VB ?

11. Static class members

12. static class data in ActiveX DLLs?

 

 
Powered by phpBB® Forum Software