Forum Discussion
The tests we run are completely data driven using DDT with an excel spreadsheet. (Hope they don't get rid of that ;-) The test server that needed to be converted was completely written in VB but fortunately the test clients were written using jscript. The test server had over 40 ODT classes. Several classes were fairly large with 30+ properties and 20+ methods. A good number of methods would also use ODT.Classes.NewArray().
So, in order, here is a high level view of what I did. Of course I would continually test the test code after each change.
1. Got rid of all uses "Owner.Owner." Used module level variables and/or functions to pass data from a parent class to a child class.
2. Made sure the organization of routines was such that all routines for a class were co-located. This does not include UserForm event modules that need to be kept outside any class.
3. Defined a custom array class that for the most part would replace ODT.Classes.NewArray() directly. See below.
4. Converted and tested each class one at a time. To save some time I simply used the Public keyword for public variables instead of defining the full Let/Get definitions. (Let/Get are done under the hood anyway.) When you test each class you will most likely discover something you forgot to convert. Each class most likely will need an accessor function outside the class so other "units" can instantiate an object based on that class.
Once you have completed the conversion for all ODT classes and arrays, and your test code still run OK, you can remove ODT from your project.
Class CustomArray
' --- Private properties
Private m_Array()
Private m_Count
Private m_IsObject ' basic type of 1st item - object or non object
' --- Public Properties
Public Property Let Count(value)
m_Count = value
End Property
Public Property Get Count()
Count = m_Count
End Property
' Read only
Public Property Get IsObject()
IsObject = m_IsObject
End Property
' --- Private Methods
Private Function UseSet(ArrayItemToCheck)
Dim strTemp
Dim blnUnexpectType
blnUnexpectType = False
UseSet = False
Select Case VarType(ArrayItemToCheck)
Case 0
strTemp = "vbEmpty"
blnUnexpectType = True
Case 1
strTemp = "vbNull"
blnUnexpectType = True
Case 2
strTemp = "vbInteger"
Case 3
strTemp = "vbLong"
Case 4
strTemp = "vbSingle"
Case 5
strTemp = "vbDouble"
Case 6
strTemp = "vbCurrency"
Case 7
strTemp = "vbDate"
Case 8
strTemp = "vbString"
Case 9
strTemp = "vbObject"
UseSet = True
Case 10
strTemp = "vbError"
blnUnexpectType = True
Case 11
strTemp = "vbBoolean"
Case 12
strTemp = "vbVariant"
Case 13
strTemp = "vbDataObject"
UseSet = True
Case 14
strTemp = "vbDecimal"
Case 17
strTemp = "vbByte"
Case 8192
strTemp = "vbArray"
UseSet = True
Case Else
strTemp = "undetected: " & VarType(ArrayItemToCheck)
blnUnexpectType = True
End Select
If blnUnexpectType Then
Log.Error("Unexpected array item type: " & strTemp)
End If
End Function
Private Sub Class_Initialize()
m_Count = 0
m_IsObject = False
ReDim m_Array(-1)
'Log.Message("UBound=" & UBound(m_Array))
End Sub
' --- Public Methods
Public Function GetArray()
GetArray = m_Array
End Function
' Returns a specific array item
Public Default Function Items(index)
If index < m_Count And index > -1 Then
If m_IsObject Then
Set Items = m_Array(index)
Else
Items = m_Array(index)
End If
Else
Log.Error("index out of range")
End If
End Function
' Used to change value of an existing item
Public Function SetItem(index, value)
if m_IsObject Then
Set m_Array(index) = value
Set SetItem = m_Array(index)
Else
m_Array(index) = value
SetItem = m_Array(index)
End If
End Function
Public Function AddItem(value)
If m_Count = 0 Then
If UseSet(value) Then
m_IsObject = True
Else
m_IsObject = false
End If
Else
If UseSet(value) <> m_IsObject Then
Log.Error("Attempted to mix objects and non objects into the array. Item NOT added!")
Exit Function
End If
End If
ReDim Preserve m_Array(m_Count)
'Log.Message("UBound=" & UBound(m_Array))
if m_IsObject Then
Set m_Array(m_Count) = value
Set AddItem = m_Array(m_Count)
Else
m_Array(m_Count) = value
AddItem = m_Array(m_Count)
End If
m_Count = m_Count + 1
End Function
Public Sub DeleteItem(index)
Dim index2
If index < m_Count and index > -1 Then
If m_IsObject Then
Set m_Array(index) = Nothing
Else
m_Array(index) = vbNull
End If
if m_Count > 1 And index < (m_Count - 1) Then
' Collapse array at the element that is being removed
index2 = index + 1
While index2 < m_Count
If m_IsObject Then
Set m_Array(index2 -1) = m_Array(index2)
Else
m_Array(index2 -1) = m_Array(index2)
End IF
index2 = index2 + 1
Wend
'ElseIf index = 0 And m_Count = 1 Then ' We must be removing the 1st and only element
' Do nothing as the adjustment of m_Count and the ReDim will suffice
'Else ' We must be removing the last element
' Do nothing as the adjustment of m_Count and the ReDim will suffice
End If
m_Count = m_Count - 1
Redim Preserve m_Array(m_Count - 1)
'Log.Message("UBound=" & UBound(m_Array))
Else
Log.Error("index out of range")
End If
End Sub
End Class
Function NewArray()
Set NewArray = New CustomArray
End Function
Related Content
- 2 years ago
- 5 years ago
Recent Discussions
- 2 days ago
- 2 days ago
- 5 days ago