VERSION 1.0 CLASS BEGIN MultiUse = -1 'True END Attribute VB_Name = "Counter" Attribute VB_GlobalNameSpace = False Attribute VB_Creatable = False Attribute VB_PredeclaredId = True Attribute VB_Exposed = False '------------------------------------------------------------------------------- 'File : Counter.cls ' http://wiki.yaslaw.info/dokuwiki/doku.php/vba/ 'Version : 1.0.0 'Name : Counter 'Author : Stefan Erb (ERS) 'History : 05.04.2017 - ERS - Creation '------------------------------------------------------------------------------- Option Explicit Const C_DEFAULT_START = 0 Const C_DEFAULT_STEP = 1 Private pStart As Long Private pCurrent As Long Private pStep As Long Private pNewEnum As Collection '------------------------------------------------------------------------------- ' -- Public Constructors '------------------------------------------------------------------------------- '/** ' * Eine neue Instanz der Klasse. Ist auch als Default definiert ' * @param Long StartWert ' * @param Long Schrittgrösse ' * @return Counter Die Singleton-Instanz '*/ Public Function instance(Optional ByVal iStart As Long = C_DEFAULT_START, Optional ByVal iStep As Long = C_DEFAULT_STEP) As Counter Attribute instance.VB_UserMemId = 0 'Attribute instance.VB_UserMemId = 0 Set instance = New Counter instance.initialize iStart, iStep End Function '/** ' * Eine neue Instanz der Klasse welche die Settings eines anderen Counters übernimmt ' * @param Counter StartWert ' * @return Counter Die Singleton-Instanz '*/ Public Function copyOf(ByRef iCounter As Counter) As Counter Set copyOf = New Counter copyOf.initialize iCounter.start, iCounter.step End Function '------------------------------------------------------------------------------- ' -- Public Methodes '------------------------------------------------------------------------------- '/** ' * Setzt die StartWerte für eine Instanz ' * @param Long StartWert ' * @param Long Schrittgrösse ' * @return Counter Die Singleton-Instanz '*/ Public Sub initialize(Optional ByVal iStart As Long = C_DEFAULT_START, Optional ByVal iStep As Long = C_DEFAULT_STEP) pStart = iStart pCurrent = iStart pStep = iStep Set pNewEnum = New Collection pNewEnum.add iStart End Sub '------------------------------------------------------------------------------- ' -- COLLECTION METHODES --- ' http://msdn.microsoft.com/en-us/library/aa262338%28v=vs.60%29.aspx '------------------------------------------------------------------------------- '/** ' * Der NewEnum wird für die For Each.. Next Schleife verwendet ' * ' * Diese Funktion hat das Attribut "'Attribute NewEnum.VB_UserMemId = -4" ' * !! Diese Iterierung hat keinen Einfluss auf die aktuelle Position !! ' * ' * @return Das nächste element ' */ Public Function NewEnum() As IUnknown Attribute NewEnum.VB_UserMemId = -4 'Attribute NewEnum.VB_UserMemId = -4 Set NewEnum = pNewEnum.[_NewEnum] End Function '/** ' * Zählt um STEP hoch und gibt den neuen Wert zurück ' * @return Long ' */ Public Function toNext() As Long pCurrent = pCurrent + pStep toNext = pCurrent pNewEnum.add pCurrent End Function '/** ' * Führt toNext bis zu maximal dem mitgegeben Wert ' * @param Long ' * @return Long ' */ Public Function toMax(ByVal iMaxValue As Long) As Long Do While (toNext + pStep) < iMaxValue Loop toMax = pCurrent End Function '/** ' * Setzt alles auf Start zurück ' */ Public Sub reset() initialize pStart, pStep End Sub '------------------------------------------------------------------------------- ' -- Public Properties '------------------------------------------------------------------------------- '/** ' * der Aktuelle Wert ' * @return Long ' */ Public Property Get current() As Long current = pCurrent End Property Public Property Get start() As Long start = pStart End Property Public Property Get step() As Long step = pStep End Property '------------------------------------------------------------------------------- ' -- Private Events '------------------------------------------------------------------------------- '/** ' * Initialisiert die Klasse ' */ Private Sub Class_Initialize() initialize End Sub