patternMinor
Revisited IsTypeSafe method implementation for "type-safe" List
Viewed 0 times
methodrevisitedistypesafetypeforsafeimplementationlist
Problem
Following up on List implementation for VB6/VBA, I'd like some thoughts about the revisited
The previous version pretty much implemented VB.net's
Given the following private type where
The
`Public Function IsTypeSafe(value As Variant) As Boolean
'Determines whether a value can be safely added to the List.
IsTypeSafe = this.TItem = vbNullString Or this.TItem = TypeName(value)
If IsTypeSafe Or this.OptionStrict Then Exit Function
Select Case this.TItem
Case "String":
IsTypeSafe = IsSafeString(value)
If IsTypeSafe Then value = CStr(value)
Case "Boolean"
IsTypeSafe = IsSafeBoolean(value)
IsTypeSafe function, below.The previous version pretty much implemented VB.net's
Option Strict setting, by only allowing widening numeric type conversions, and preventing implicit conversion between numeric and string data types. This one is more permissive, to a point where I'm torn between ease of use (VB6 isn't .net!) and correctness (it's twisting Option Strict by allowing implicit conversions of in-range values, i.e. a Long with a value of 32 could be added to a List... does that help or hinder usability?).Given the following private type where
TItem is the type name of T in List...Private Type tList
Encapsulated As Collection
TItem As String
OptionStrict As Boolean
End Type
Private this As tList
The
IsTypeSafe method's behavior depends on the values of this.OptionStrict and this.TItem:- When this.OptionStrict = True (the default value; can be modified through a public property), then
IsTypeSafeonly returnsTruewhen the type ofvaluematches exactly with that ofthis.TItem.
- When this.OptionStrict = False, then
IsTypeSafereturnsTrueifvaluecan be legally converted to the type specified bythis.TItem; if the value can be converted, it is converted, so as to avoid having aListwithBytevalues...
- When this.TItem = vbNullString, then
IsTypeSafereturnsTruesystematically, and thenthis.TItembecomes the type name ofvalue.
`Public Function IsTypeSafe(value As Variant) As Boolean
'Determines whether a value can be safely added to the List.
IsTypeSafe = this.TItem = vbNullString Or this.TItem = TypeName(value)
If IsTypeSafe Or this.OptionStrict Then Exit Function
Select Case this.TItem
Case "String":
IsTypeSafe = IsSafeString(value)
If IsTypeSafe Then value = CStr(value)
Case "Boolean"
IsTypeSafe = IsSafeBoolean(value)
Solution
This code looks very clean,
It looks like it handles edge cases well.
I especially like the way that if it converts to the specified data type that it does it and doesn't make you convert explicitly. that is very slick!
I would definitely like to see some code that puts it into play.
Not much to review here, but very good code and good syntax.
Edit
I did find something that I might do differently.
Instead of
I would make it more like a
Perhaps something like this
I think this is a lot clearer as you are not really skating around the errors, and this way you can add an
I like the
It looks like it handles edge cases well.
I especially like the way that if it converts to the specified data type that it does it and doesn't make you convert explicitly. that is very slick!
I would definitely like to see some code that puts it into play.
Not much to review here, but very good code and good syntax.
Edit
I did find something that I might do differently.
Instead of
On Error Resume Next I would make it more like a
Try CatchPerhaps something like this
Private Function IsSafeDate(value As Variant) As Boolean
'On Error Resume Next
On Error GoTo ErrHandler
Dim result As Date
result = CDate(value) 'assigning value would be an undesirable side-effect here!
IsSafeDate = True
Exit Function
ErrHandler:
IsSafeDate = False
Err.Clear
End FunctionI think this is a lot clearer as you are not really skating around the errors, and this way you can add an
Exit Sub code too if you need it.I like the
Try Catch Method better myself.Code Snippets
Private Function IsSafeDate(value As Variant) As Boolean
'On Error Resume Next
On Error GoTo ErrHandler
Dim result As Date
result = CDate(value) 'assigning value would be an undesirable side-effect here!
IsSafeDate = True
Exit Function
ErrHandler:
IsSafeDate = False
Err.Clear
End FunctionContext
StackExchange Code Review Q#33035, answer score: 7
Revisions (0)
No revisions yet.