HiveBrain v1.2.0
Get Started
← Back to all entries
patternMinor

Revisited IsTypeSafe method implementation for "type-safe" List

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
methodrevisitedistypesafetypeforsafeimplementationlist

Problem

Following up on List implementation for VB6/VBA, I'd like some thoughts about the revisited 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 IsTypeSafe only returns True when the type of value matches exactly with that of this.TItem.



  • When this.OptionStrict = False, then IsTypeSafe returns True if value can be legally converted to the type specified by this.TItem; if the value can be converted, it is converted, so as to avoid having a List with Byte values...



  • When this.TItem = vbNullString, then IsTypeSafe returns True systematically, and then this.TItem becomes the type name of value.



`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 On Error Resume Next
I would make it more like a Try Catch

Perhaps 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 Function


I 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 Function

Context

StackExchange Code Review Q#33035, answer score: 7

Revisions (0)

No revisions yet.