aparicion25
asked on
Static Variables
Hi:
I'm trying to initialize a static variable.
I need this collection class to generate account numbers starting from
10000000 instead of starting from 0.
I would appreciate if someone could steer me in the right direction.
Thanks
*************************T his is a visual basic Collection
class******************
Option Explicit
Private mAccountItems As Collection
Private Sub Class_Initialize()
'Create collection object
Set mAccountItems = New Collection
End Sub
Private Sub Class_Terminate()
'Release the collection reference
Set mAccountItems = Nothing
End Sub
Private Function NextID() As String
'Assign the next ID
Static intID As Integer
intID = intID + 1
NextID = Trim(Str(intID)) 'Convert to string
End Function
Public Sub Add(ByVal intAccountTypeID As Integer, ByVal intAccountBalance As
Currency, ByVal intMinimumBalance As Currency)
'Add a new member to the collection
'Object variable to hold the new object
On Error Resume Next
Dim NewAccount As New CAccount
With NewAccount 'set up the properties for the new object
'Call the function to assign the next key
.ID = NextID
.TypeID = intAccountTypeID
.Balance = intAccountBalance
.MinimumBalance = intMinimumBalance
mAccountItems.Add NewAccount, .ID
End With
End Sub
Public Sub Remove(ByVal strkey As String)
'Remove a member from the collection
On Error Resume Next
mAccountItems.Remove strkey
End Sub
Public Function Item(ByVal strkey As String) As CAccount
'Return on member from the collection
On Error Resume Next
Set Item = mAccountItems.Item(strkey)
End Function
Public Property Get Count() As Long
'Return the number of members in the collection
On Error Resume Next
Count = mAccountItems.Count
End Property
*******************This is the class itsef********************* **
Option Explicit
Private mstrID As String
Private mintAccountTypeID As Integer
Private mintAccountBalance As Currency
Private mintMinimumBalance As Currency
Event LowBalance()
Public Property Get ID() As String
'Retrieve the current value
ID = mstrID
End Property
Public Property Let ID(ByVal strID As String)
'Assign the property value
mstrID = strID
End Property
Public Property Get TypeID() As Integer
'Retrieve the current value
TypeID = mintAccountTypeID
End Property
Public Property Let TypeID(ByVal intTypeID As Integer)
'Assign the property value
mintAccountTypeID = intTypeID
End Property
Public Property Get Balance() As Currency
'Retrieve the current value
Balance = mintAccountBalance
End Property
Public Property Let Balance(ByVal intBalance As Currency)
'Assign the property value
mintAccountBalance = intBalance
If mintAccountBalance < mintMinimumBalance Then
RaiseEvent LowBalance
End If
End Property
Public Property Get MinimumBalance() As Currency
'Retrieve the current value
MinimumBalance = mintMinimumBalance
End Property
Public Property Let MinimumBalance(ByVal intMinimumBalance As Currency)
'Assign the property value
mintMinimumBalance = intMinimumBalance
End Property
I'm trying to initialize a static variable.
I need this collection class to generate account numbers starting from
10000000 instead of starting from 0.
I would appreciate if someone could steer me in the right direction.
Thanks
*************************T
class******************
Option Explicit
Private mAccountItems As Collection
Private Sub Class_Initialize()
'Create collection object
Set mAccountItems = New Collection
End Sub
Private Sub Class_Terminate()
'Release the collection reference
Set mAccountItems = Nothing
End Sub
Private Function NextID() As String
'Assign the next ID
Static intID As Integer
intID = intID + 1
NextID = Trim(Str(intID)) 'Convert to string
End Function
Public Sub Add(ByVal intAccountTypeID As Integer, ByVal intAccountBalance As
Currency, ByVal intMinimumBalance As Currency)
'Add a new member to the collection
'Object variable to hold the new object
On Error Resume Next
Dim NewAccount As New CAccount
With NewAccount 'set up the properties for the new object
'Call the function to assign the next key
.ID = NextID
.TypeID = intAccountTypeID
.Balance = intAccountBalance
.MinimumBalance = intMinimumBalance
mAccountItems.Add NewAccount, .ID
End With
End Sub
Public Sub Remove(ByVal strkey As String)
'Remove a member from the collection
On Error Resume Next
mAccountItems.Remove strkey
End Sub
Public Function Item(ByVal strkey As String) As CAccount
'Return on member from the collection
On Error Resume Next
Set Item = mAccountItems.Item(strkey)
End Function
Public Property Get Count() As Long
'Return the number of members in the collection
On Error Resume Next
Count = mAccountItems.Count
End Property
*******************This is the class itsef*********************
Option Explicit
Private mstrID As String
Private mintAccountTypeID As Integer
Private mintAccountBalance As Currency
Private mintMinimumBalance As Currency
Event LowBalance()
Public Property Get ID() As String
'Retrieve the current value
ID = mstrID
End Property
Public Property Let ID(ByVal strID As String)
'Assign the property value
mstrID = strID
End Property
Public Property Get TypeID() As Integer
'Retrieve the current value
TypeID = mintAccountTypeID
End Property
Public Property Let TypeID(ByVal intTypeID As Integer)
'Assign the property value
mintAccountTypeID = intTypeID
End Property
Public Property Get Balance() As Currency
'Retrieve the current value
Balance = mintAccountBalance
End Property
Public Property Let Balance(ByVal intBalance As Currency)
'Assign the property value
mintAccountBalance = intBalance
If mintAccountBalance < mintMinimumBalance Then
RaiseEvent LowBalance
End If
End Property
Public Property Get MinimumBalance() As Currency
'Retrieve the current value
MinimumBalance = mintMinimumBalance
End Property
Public Property Let MinimumBalance(ByVal intMinimumBalance As Currency)
'Assign the property value
mintMinimumBalance = intMinimumBalance
End Property
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Yeah...this should probably be defined as a module-level variable as described above. The use of static variables is, in my opinion dangerous and troublesome (because you're not in control of initializing it and it's scope is beyond the current procedure anyway.)
However, if you want to do it within the procedure, just check the value right off:
Private Function NextID() As String
'Assign the next ID
Static intID As Integer
' Add this
const cStartingValue as long = 10000000
if intID = 0 then
intID = cStartingValue
end if
' End of Add
intID = intID + 1
NextID = Trim(Str(intID)) 'Convert to string
End Function
However, if you want to do it within the procedure, just check the value right off:
Private Function NextID() As String
'Assign the next ID
Static intID As Integer
' Add this
const cStartingValue as long = 10000000
if intID = 0 then
intID = cStartingValue
end if
' End of Add
intID = intID + 1
NextID = Trim(Str(intID)) 'Convert to string
End Function
Private mlID as Long