?
Solved

Static Variables

Posted on 2003-03-08
3
Medium Priority
?
199 Views
Last Modified: 2010-05-01
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

*************************This 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




0
Comment
Question by:aparicion25
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 3

Accepted Solution

by:
phildaley earned 150 total points
ID: 8095860
Instead on declaring a static in NewID, declare a module level variable in the class. Also, it needs to be declared as Long and not Integer or you'll get an overflow error. Set the inital default in Initialize event. Example:

.
.
Private mAccountItems As Collection
Private mlNextID as Long
.
.
Private Sub Class_Initialize()
   'Create collection object
   Set mAccountItems = New Collection
   mlID = 10000000
End Sub
.
.
Private Function NextID() As String
   'Assign the next ID
   mlID = mlID + 1
   NextID = CStr(mlID) 'Convert to string
End Function
.
.


0
 
LVL 3

Expert Comment

by:phildaley
ID: 8095867
whoops - it should have been declared as
Private mlID as Long
0
 
LVL 22

Expert Comment

by:rspahitz
ID: 8096320
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
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses
Course of the Month10 days, 16 hours left to enroll

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question