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

Class: 2D Variant Array

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

Problem

Note: Yes. It's big. I'm not expecting commensurately
long/detailed answers (though if anyone wants to write one, you'll definitely be receiving a substantial bounty). This class is going to be used a lot in my
VBA development so any reviews at all would be immensely helpful.
Even if it's just a typo somewhere or an edge case that's not being
checked or functionality you think should be added to it or even just
a Gut-Check on coding smells, readability and the like.

If you want a paste-able version of this code, please see this github repo

I do a lot of data analysis with spreadsheets. VBA has no in-built array functions (sorting, filtering etc.). This is a problem.

So, I took my accumulated collection of Array-manipulation methods, cleaned them up and turned them into a Class: CLS_2D_VarArray.

It is also supposed to be paired with my collection of Standard Methods, in a Base_Standard_Methods Module, and with CLS_Comparison_Predicate which is used to pass logical expressions to functions.

I would love to get peoples' thoughts on it.

Class-Level stuff:

Type of Array:

I only use 2-D Variant Arrays, declared thus:

Dim arr As Variant
Redim arr(1 to 5, 1 to 5)


Only declared that way for various reasons which I won't go into here.

Only 2-Dimensional because that covers 95% of my use-cases, and supporting multi-dimensional operations would cause a lot of additional complexity.

Properties:

Private Type TVarArray
    varArray As Variant
    ColumnHeaderIndexes As Dictionary '/ Set when SetArray is called with hasHeaders = True
    PrintRange As Range '/ Set whenever Me.PrintToSheet is called
End Type
Private This As TVarArray


Behaviour:

All the functions are designed to be chain-able. So, with the exception of CopyArray(), which returns a copy of VarArray, or GetArray(), which returns VarArray itself, all functions return a new Class object.

E.G. I can do the following:

```
Set filteredArray = baseClass.RemoveIndexes(

Solution

This if isn't the easiest to understand

If Not ((checkDimension = 1 And checkIndex >= LB1 And checkIndex = LB2 And checkIndex <= UB2)) Then


I get that it's if not either of these two sets - like this, right?

If _
(Not checkDimension = 1 And Not checkIndex >= LB1 And Not checkIndex = LB2 And Not checkIndex <= UB2) Then


Honestly this might be a time to use that underscore to break something up that, in reality, doesn't need to be broken up - just so it's more clear what the conditions are. Or maybe doing it weird like

Dim firstCondition As Boolean
Dim secondCondition As Boolean

If Not checkDimension = 1 And Not checkIndex >= LB1 And Not checkIndex = LB2 And Not checkIndex <= UB2 Then secondCondition = True

If firstCondition Or secondCondition Then


Or at least

If Not (checkDimension = 1 And checkIndex >= LB1 And checkIndex = LB2 And checkIndex <= UB2) Then secondCondition = True


Also, since the answer is already here, you say this twice -

PrintErrorMessage "Target Index does not exist"


Looks like a constant string could be of use ;)

Code Snippets

If Not ((checkDimension = 1 And checkIndex >= LB1 And checkIndex <= UB1) Or (checkDimension = 2 And checkIndex >= LB2 And checkIndex <= UB2)) Then
If _
(Not checkDimension = 1 And Not checkIndex >= LB1 And Not checkIndex <= UB1) _
Or _
(Not checkDimension = 2 And Not checkIndex >= LB2 And Not checkIndex <= UB2) Then
Dim firstCondition As Boolean
Dim secondCondition As Boolean

If Not checkDimension = 1 And Not checkIndex >= LB1 And Not checkIndex <= UB1 Then firstCondition = True
If Not checkDimension = 2 And Not checkIndex >= LB2 And Not checkIndex <= UB2 Then secondCondition = True

If firstCondition Or secondCondition Then
If Not (checkDimension = 1 And checkIndex >= LB1 And checkIndex <= UB1) Then firstCondition = True
If Not (checkDimension = 2 And checkIndex >= LB2 And checkIndex <= UB2) Then secondCondition = True
PrintErrorMessage "Target Index does not exist"

Context

StackExchange Code Review Q#129114, answer score: 2

Revisions (0)

No revisions yet.