Well it appears that a static variable is shared across all instances
(based on some simple test code).
I think this makes sense because if you wanted one per instance you
could just use a private property.
There is also the shared property which is shared across all
instances but may be more convenient than a static variable.
If you need to store the VIN then use a private property but you can
use a shared property to do the incrementing on. In the Car
constructor, increment the shared property then copy it to the
private property. Having the shared property lets you access the
counter outside of the constructor (the static variable can only be
accessed within the function).
I hope that helps.
Cheers,
Malcolm
On Jan 9, 2007, at 5:08 PM, Tom Benson wrote:
I honestly don't know, but I'd be really surprised if static
variables were not instance bound.
If I increment a static variable in one object instance, I would
not likely want it to be increased in all other instances of that
object.
The above is standard behaviour for most other languages I can
think of.
If you truly want a unique ID for each insance of a given object,
I'd be using .hash or even an MD5 of it's .hash property
- Tom
On 10/01/2007, at 11:55 AM, William Squires wrote:
Okay, I know that each instance of a class shares all the methods
defined by that class (i.e. any instance can call such a method),
and that each instance gets its own copy of each property that has
the same name (i.e. a property CarColor As Color would be
accessible by an instance of the Car class with Me.CarColor = RGB
(255, 0, 0) for example, and each one has its own value. But what
about a static variable in a method?
Class Car
Public Property CarColor As Color
Sub Car()
CarColor = RGB(0, 0, 0) // cars start out basic black
End Sub
Function StupidVIN() As Integer
Static myUniqueID As Integer = 0
Dim result As Integer
result = myUniqueID
myUniqueID = myUniqueID + 1
Return result
End Function
End Class
...
Dim a, b As Car
a = New Car()
b = New Car()
a.CarColor = RGB(255, 0, 0) // I want mine in red!
MsgBox "a's unique ID: " + CStr(a.StupidVIN())
MsgBox "b's unique ID: " + CStr(b.StupidVIN())
Should both print the same value (because a and b are separate
objects in memory), or a different value (because the static
variable is shared per-method and thus increments itself
regardless of whether called from a or b)?
_______________________________________________
Unsubscribe or switch delivery mode:
<http://www.realsoftware.com/support/listmanager/>
Search the archives of this list here:
<http://support.realsoftware.com/listarchives/lists.html>
_______________________________________________
Unsubscribe or switch delivery mode:
<http://www.realsoftware.com/support/listmanager/>
Search the archives of this list here:
<http://support.realsoftware.com/listarchives/lists.html>
_______________________________________________
Unsubscribe or switch delivery mode:
<http://www.realsoftware.com/support/listmanager/>
Search the archives of this list here:
<http://support.realsoftware.com/listarchives/lists.html>