static variables 
Author Message
 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 strating 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



Thu, 25 Aug 2005 04:58:08 GMT  
 static variables
Way more code than was necessary. Change the variable type for intID to
Long and add the line shown...

Quote:
> Private Function NextID() As String
>     'Assign the next ID

      Static intID As Long
      If intID = 0 then intID = 9999999

Quote:
>     intID = intID + 1
>     NextID = Trim(Str(intID)) 'Convert to string
> End Function

Rick - MVP


Thu, 25 Aug 2005 09:03:39 GMT  
 static variables
I haven't read other post on this thread but there is a gotcha using Static varibles like this..
If you are assigning these to an account in a database then you'll need some form of checking if the NextID
is an unused number and if it is used then call NextID again and again and again and etc...
This is because each time you run your program the first ID will always be 9999999...
If this is for database operations it would be better to have intID defined at the module level and then
assigned the last value used when the program frist starts up...

Just food for thought...

Quote:

>Way more code than was necessary. Change the variable type for intID to
>Long and add the line shown...

>> Private Function NextID() As String
>>     'Assign the next ID

>      Static intID As Long
>      If intID = 0 then intID = 9999999

>>     intID = intID + 1
>>     NextID = Trim(Str(intID)) 'Convert to string
>> End Function

>Rick - MVP

Have a good day...

Don



Thu, 25 Aug 2005 10:31:12 GMT  
 static variables
BTW: I am assuming that this will be a single user program..
If not then you'll have to add a read/increment/rewrite to the database...
Oh such fun and games we have to play... ;->

Quote:

>I haven't read other post on this thread but there is a gotcha using Static varibles like this..
>If you are assigning these to an account in a database then you'll need some form of checking if the NextID
>is an unused number and if it is used then call NextID again and again and again and etc...
>This is because each time you run your program the first ID will always be 9999999...
>If this is for database operations it would be better to have intID defined at the module level and then
>assigned the last value used when the program frist starts up...

>Just food for thought...


>>Way more code than was necessary. Change the variable type for intID to
>>Long and add the line shown...

>>> Private Function NextID() As String
>>>     'Assign the next ID

>>      Static intID As Long
>>      If intID = 0 then intID = 9999999

>>>     intID = intID + 1
>>>     NextID = Trim(Str(intID)) 'Convert to string
>>> End Function

>>Rick - MVP

>Have a good day...

>Don

Have a good day...

Don



Thu, 25 Aug 2005 10:39:13 GMT  
 static variables

Quote:
> I haven't read other post on this thread but there is a gotcha using

Static varibles like this..
Quote:
> If you are assigning these to an account in a database then you'll

need some form of checking if the NextID
Quote:
> is an unused number and if it is used then call NextID again and again

and again and etc...
Quote:
> This is because each time you run your program the first ID will

always be 9999999...

Good point! As a matter of fact, if the OP wants to "update" the
starting value for the intID variable by recalling a previously saved
maximum value from the registry or an INI file, then intID can't be
Static... it will have to be a true variable.

Rick - MVP

Quote:
> If this is for database operations it would be better to have intID

defined at the module level and then
Quote:
> assigned the last value used when the program frist starts up...

> Just food for thought...

> On Sat, 8 Mar 2003 20:03:39 -0500, "Rick Rothstein"

> >Way more code than was necessary. Change the variable type for intID
to
> >Long and add the line shown...

> >> Private Function NextID() As String
> >>     'Assign the next ID

> >      Static intID As Long
> >      If intID = 0 then intID = 9999999

> >>     intID = intID + 1
> >>     NextID = Trim(Str(intID)) 'Convert to string
> >> End Function

> >Rick - MVP

> Have a good day...

> Don



Thu, 25 Aug 2005 13:25:59 GMT  
 static variables

Quote:
> Good point! As a matter of fact, if the OP wants to "update" the
> starting value for the intID variable by recalling a previously saved
> maximum value from the registry or an INI file, then intID can't be
> Static... it will have to be a true variable.

Forget that last conclusion of mine... of course it can be Static. I
read Static and for some reason thought Const... go figure.

Rick - MVP



Thu, 25 Aug 2005 16:34:43 GMT  
 static variables
Thanks for all your comments and inputs, I really appreciate it. I finally
got my program running.
Thanks

Quote:
> Hi:
> I'm trying to initialize a static variable.
> I need this collection class to generate account numbers starting from
> 10000000 instead of strating 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



Thu, 25 Aug 2005 22:21:58 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. Static variable not static enough ???

2. Static variables are not supported...

3. Static variables in VBS?

4. Static Variable (VB)

5. Static variables...when to use?

6. Too many local, non-static variables

7. "Permanently" static variables

8. Would like to get value from local static variable

9. Newbie: static variable initialization

10. Static variables/scope - "Accumulate"?

11. Static variables - simpler question

 

 
Powered by phpBB® Forum Software