Can I ReDim a module level array using a property? - arrays

I think I have a pretty good handle on how to handle module level arrays in VBA though Property Get and Let. Is there a way to ReDim a module level array through a property?
The following code errors out at the ReDim statement in the last procedure (DoTest).
Private mstrTestArray() As String
Private Sub Class_Initialize()
ReDim mstrTestArray(0) As String
End Sub
Private Property Get TestArray() As String()
TestArray = mstrTestArray
End Property
Private Property Let TestArray(ByRef strTestArray() As String)
mstrTestArray = strTestArray
End Property
Private Property Get TestArrayValue(d1 As Long) As String
TestArrayValue = mstrTestArray(d1)
End Property
Private Property Let TestArrayValue(d1 As Long, strValue As String)
mstrTestArray(d1) = strValue
End Property
Sub DoTest()
Dim intCharCode As Integer
For intCharCode = 97 To 122
If Not Len(TestArrayValue(UBound(TestArray))) > 0 Then
TestArrayValue(UBound(TestArray)) = Chr(intCharCode)
Else
ReDim Preserve TestArray(UBound(TestArray) + 1) As String
TestArrayValue(UBound(TestArray)) = Chr(intCharCode)
End If
Next intCharCode
Debug.Print TestArrayValue(LBound(TestArray)) _
& " through " _
& TestArrayValue(UBound(TestArray))
End Sub
Thanks!

This is a good question. I'll answer your question directly at the bottom, but let's start with a brief background of Object-Oriented Programming in VBA. In most object-oriented languages, a property will often look like a field, but act like a method. What does this mean?
When you instantiate a class and set a value to a property, it looks like this:
Sub TestMyClass()
Dim mc As MyClass
Set mc = new MyClass
mc.MyProperty = 1
End Sub
In the above code, MyProperty looks like a field, right? But let's look at how it's defined in the class:
Private pMyProperty As Integer
Public Property Get MyProperty() As Integer
MyProperty = pMyProperty
End Property
Public Property Let MyProperty(lMyProperty As Integer)
pMyProperty = lMyProperty
End Property
As you can see in the above code, while pMyProperty is an Integer field, the public Get and Set methods for MyProperty actually look more like methods. A property "wraps" around a field and is especially helpful in setting access to the underlying field.
In your example, you were trying to ReDim a property that returns a reference to the array. I'm not 100% sure, but I don't think you can use ReDim on a reference of an array.
I changed your code to modify the actual Private field mstrTestArray and it seemed to work fine. Is that something you can try?
Sub DoTest()
Dim intCharCode As Integer
For intCharCode = 97 To 122
If Not Len(TestArrayValue(UBound(TestArray))) > 0 Then
TestArrayValue(UBound(TestArray)) = Chr(intCharCode)
Else
ReDim Preserve mstrTestArray(UBound(mstrTestArray) + 1) As String
TestArrayValue(UBound(TestArray)) = Chr(intCharCode)
End If
Next intCharCode
Debug.Print TestArrayValue(LBound(TestArray)) _
& " through " _
& TestArrayValue(UBound(TestArray))
End Sub

Related

Storing & recovering 2-dimensional Array in a Class

I just recently moved from VB6 to VB.NET and I'm recoding an old app. So I'm pretty unexperienced with .NET so far.
I have multiple (lets say 4 in this code example) twodimensional string arrays (or actually an array of arrays) which I want to store as a ComboBox items ie. one twodimensional array is one item.
Public Class MyItem
Private sName As String
Private sArr As Array()
Public Sub New(ByVal Name As String, ParamArray Arr As Array())
sName = Name
sArr = Arr
End Sub
Public Property Arr() As Array()
Get
Return sArr
End Get
Set(ByVal sValue As Array())
sArr = sValue
End Set
End Property
Public Overrides Function ToString() As String
Return sName
End Function
End Class
---
Dim sMyArray as Array()
For i As Integer = 0 to 3
sMyArray = Nothing ' resetting the array before refilling it
'
' No code here but filling sMyArray by reading a text file, each line
' number as dim 1 and splitted each line into dim 2 with ";" using Split(sRead, ";")
' so Debub.Print(sMyArray(0)(0)) prints the beginning of the first line until first ";" <- this works fine
'
' Then passing sMyArray to a ComboBox item
'
ComboBox.Items.Add(New MyItem("item" & i, sMyArray))
Next i
The problem is that when recovering the arrays from ComboCox items only the last ComboBox item has array data. So for example
Dim sMyNewArray As Array() = ComboBox.Items.Item(0).Arr
Debug.Print(sMyNewArray(0)(0))
throws an error while
Dim sMyNewArray As Array() = ComboBox.Items.Item(3).Arr
Debug.Print(UBound(sMyNewArray(UBound(sMyNewArray))))
does not and prints the last item's last row's ubound
Can anyone figure out what is it I'm missing or tell me a better way to do this? I'm pretty sure there is one..
I'm not 100% sure, but I think the problem is in this section:
Dim sMyArray as Array()
For i As Integer = 0 to 3
sMyArray = Nothing ' resetting the array before refilling it
Arrays are technically reference types, but like strings, there's some extra compiler magic to make them feel at times more like value types, and I have a sense in this case the actual sMyArray reference was used (perhaps because of a ParamArrays optimzation), such that setting it to Nothing broke things. The more idiomatic way to write this code for .Net it like this:
For i As Integer = 0 to 3
Dim sMyArray as Array()
.Net has a much more sophisticated garbage collector than was available for VB6. We don't often set variables to Nothing any more, but instead just re-assign them or let them fall out of scope. In fact, setting a variable to Nothing can in rare cases be actively harmful. Moreover, we want to see the Dim keyword inside the loop, so you're working with a different variable on each iteration, with the smallest possible scope.
While I'm here, in .Net we pretty much never use the base Array type. Instead of this:
Private sArr As Array()
You pretty much always do this:
Private arr As String()()
or this, for true two-dimensional (non-jagged) arrays:
Private arr As String(,)
or, best of all, this:
Private arr As New List(Of String())
Since VB.Net has more collection types than just array.
Also, I don't have the link handy, but Microsoft's coding guidelines now explicitly ask you not to use hungarian warts for variable and class names (so sArr can just be arr). This is a change from the VB6 era because of changes to the language where the type is more likely to be implicit with the variable and improvements to the tooling, where the prefixes usually no longer add much utility and have been shown to hurt readability.
Not really sure why you have a 2 dimensional array, but here is a small sample NOT using the Array type. It uses just plain strings and string arrays. Let me know if this helps. This splits a few strings, then reads out the results after populating.
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim sMyArray()() As String
Dim line1 As String = "a;b;c;d 1;2;3;4;5"
Dim line2 As String = "z;x;y;w 99;65;32;21;18"
sMyArray = ParseString(line1)
cboBox1.Items.Add(New MyItem("System0", sMyArray))
sMyArray = ParseString(line2)
cboBox1.Items.Add(New MyItem("System1", sMyArray))
For i As Integer = 0 To cboBox1.Items.Count - 1
For j As Integer = 0 To UBound(cboBox1.Items(i).arr)
For k As Integer = 0 To UBound(cboBox1.Items(i).arr(j))
Debug.Write(cboBox1.Items(i).arr(j)(k) & " ")
Next
Next
Debug.WriteLine("")
Next
End Sub
Private Function ParseString(s As String) As String()()
Dim rows As String() = s.Split(" ")
Dim matrix As String()() = New String(rows.Length - 1)() {}
For i As Integer = 0 To rows.Length - 1
matrix(i) = rows(i).Split(";")
Next
Return matrix
End Function
End Class
Public Class MyItem
Private sName As String
Private sArr As String()()
Public Sub New(ByVal Name As String, ByVal ParamArray Arr As String()())
sName = Name
sArr = Arr
End Sub
Public Property Arr() As String()()
Get
Return sArr
End Get
Set(ByVal sValue As String()())
sArr = sValue
End Set
End Property
Public Overrides Function ToString() As String
Return sName
End Function
End Class

Initializing Object with Arrays VBA

I'm trying to create a class with arrays in it, and I'm having issues creating the class for it...
CLASS:
Private pST(0 To 2) As String
Public Property Get ST() As String
ST() = pST()
End Property
Public Property Let ST(value() As String) '<---- ERROR HERE
pST() = value()
End Property
CODE RUN:
Sub test()
Dim foo As cPurchaseOrder
Set foo = New cPurchaseOrder
foo.ST(0) = "test"
Debug.Print foo.ST(0)
End Sub
THE ERROR:
Compile error:
Definitions of property procedures for the same property are inconsistent, or property procedure has an optional parameter, a ParamArray, or an invalid Set final parameter.
THE QUESTION:
How can I properly initialize a class with arrays as variables?
EDIT: in relation to Mat's Mug response
CLASS CHANGED:
Private pST As Variant
Public Property Get STContent(ByVal index As Long) As String
STContent = pST(index)
End Property
Public Property Let STContent(ByVal index As Long, ByVal value As String)
pST(index) = value
End Property
Private Sub Class_Initialize()
ReDim pST(0 To 2)
End Sub
CODE RUN TO TEST:
Sub test()
Dim foo As cPurchaseOrder
Set foo = New cPurchaseOrder
foo.STContent(0) = "test" '<--- Type mismatch here
Debug.Print foo.STContent(0)
End Sub
Your getter would need to return a String() array for the types to be consistent:
Public Property Get ST() As String()
However I wouldn't recommend exposing an array like this. First because assigning typed arrays is rather painful, second because the setter (Property Let) is actually cheating here:
Public Property Let ST([ByRef] value() As String)
Unless you specify ByVal explicitly, a parameter is always passed ByRef in VBA... except there's this quirk about Property Let - the RHS/value parameter is always passed ByVal at run-time.
And arrays can only ever be passed ByRef.
Therefore, a property that gets (or assigns, actually) a whole array doesn't make much sense.
A better way would be to encapsulate the array (I'd make it a Variant though), and expose its contents (not the array itself) through an indexed property:
Private internal As Variant 'String array
'...
Public Property Get Content(ByVal index As Long) As String
Content = internal(index)
End Property
Public Property Let Content(ByVal index As Long, ByVal value As String)
internal(index) = value
End Property
You have a lot of issues there.
First, your Property Get needs to return a String array. Second, your array needs to be dynamic, or you need to rewrite the whole thing so that you pass an index value to it, otherwise there is no way to indicate which value you are passing to the array. So, for example, using a dynamic array:
Private pST() As String
Public Property Get ST() As String()
ST = pST
End Property
Public Property Let ST(value() As String)
pST() = value()
End Property
and the calling code:
Sub test()
Dim foo As cPurchaseOrder
Set foo = New cPurchaseOrder
Dim asData() As String
ReDim asData(0)
asData(0) = "test"
foo.ST = asData
Debug.Print foo.ST()(0)
End Sub
Unfortunately, I couldn't be sure form the original what the intent was.
It is getting late here but give it a try. In the module:
Option Explicit
Sub Test()
Dim foo As cPurchaseOrder
Set foo = New cPurchaseOrder
foo.AddValueToSt "test", 1
Debug.Print foo.ST(1)
End Sub
In the Class:
Option Explicit
Private pST
Public Property Get ST() As Variant
ST = pST
End Property
Public Property Let ST(value As Variant)
pST = value
End Property
Public Function AddValueToSt(value As Variant, position As Long)
pST(position) = value
End Function
Private Sub Class_Initialize()
ReDim pST(2)
End Sub
This is my way to use the Factory Method Pattern. When I say "my way", for me this pattern is translated to "Whenever some OOP requires more than 5 minutes of thinking simply add a function."

How can I pass Items from a set of Class objects held in a dictionary to an array?

I am trying to make a bit of a leap in coding from just using dictionaries (which I only understand in a basic way) to using a Class object to hold data.
I have created a Class purely to hold 4 items of data which need to be held together and then referenced later. I then have code that creates a couple of these Class objects and which populate data into their 4 bits.
Here is my Class Module (called NarrativeGroup) :
Private pNarrative As String
Private pBillCat As String
Private pDateIndex As String
Private pFrequency As Long
''''''''''''''''''''''
' Narrative properties
''''''''''''''''''''''
Public Property Get Narrative() As String
Narrative = pNarrative
End Property
Public Property Let Narrative(Value As String)
pNarrative = Value
End Property
''''''''''''''''''''''
' BillCat properties
''''''''''''''''''''''
Public Property Get BillCat() As String
BillCat = pBillCat
End Property
Public Property Let BillCat(Value As String)
pBillCat = Value
End Property
''''''''''''''''''''''
' DateIndex properties
''''''''''''''''''''''
Public Property Get DateIndex() As String
DateIndex = pDateIndex
End Property
Public Property Let DateIndex(Value As String)
pDateIndex = Value
End Property
''''''''''''''''''''''
' Frequency properties
''''''''''''''''''''''
Public Property Get Frequency() As String
Frequency = pFrequency
End Property
Public Property Let Frequency(Value As String)
pFrequency = Value
End Property
I then have the following code in a normal module. I am trying to load the 4 item types into an array and then to try and test if it is working. My reason for loading it into an array is to then use a sub to output it onto a worksheet. But the code errors! :
Sub setNarrativeGroup() 'to put the dictionary of Narrative object Items into an array
Set dict_Narratives = New Scripting.Dictionary
dict_Narratives.CompareMode = TextCompare 'make text comparisons so they are not case sensitive
Dim NewNarrative As NarrativeGroup
Set NewNarrative = New NarrativeGroup
Dim array_Narratives As Variant
NewNarrative.Narrative = "fee prep"
NewNarrative.BillCat = "Billing"
NewNarrative.DateIndex = "01.2015"
NewNarrative.Frequency = 3
dict_Narratives.Add NewNarrative.Narrative, NewNarrative
NewNarrative.Narrative = "meeting"
NewNarrative.BillCat = "Trustee Meeting"
NewNarrative.DateIndex = "02.2015"
NewNarrative.Frequency = 1
dict_Narratives.Add NewNarrative.Narrative, NewNarrative
array_Narratives = dict_Narratives.Items
MsgBox array_Narratives(1, 1)
Call PrintArray(array_Narratives, "Sheet1", 1, 1)
End Sub
Sub PrintArray(Data, SheetName As String, intStartRow As Integer, intStartCol As Integer)
Dim oWorksheet As Worksheet
Dim rngCopyTo As Range
Set oWorksheet = ActiveWorkbook.Worksheets(SheetName)
' size of array
Dim intEndRow As Integer
Dim intEndCol As Integer
intEndRow = UBound(Data, 1)
intEndCol = UBound(Data, 2)
Set rngCopyTo = oWorksheet.Range(oWorksheet.Cells(intStartRow, intStartCol), oWorksheet.Cells(intEndRow, intEndCol))
rngCopyTo.Value = Data
End Sub
I have tried to search for help on working with dictionaries of class objects and spitting them out into an array, but there doesn't seem to be much out there! Any help much appreciated, and apologies for any big no-no's in my code above! :)

Can't assign array using array member variable

I have a class where one of the member variables is an array. I am trying to assign an array to the object but keep getting the 'Can't assign array' compile error. Also I was curious as how to get UBound of the array in object. UBound(obj.array) doesn't compile. I am using excel 07 vba.
'Test routine that keeps failing
Sub test()
Dim Arr(2) As String
Arr(0) = ""
Arr(1) = "Pizza"
Arr(2) = "Hoes"
Dim obj As Cats
Set obj = New Cats
obj.avry = Arr
obj.field = 4
MsgBox UBound(obj.ary)
End Sub
'Class declaration
Private pary() As String
Private pfield As Long
Public Property Get ary(ByVal index As Long) As String
Set ary = pary(index)
End Property
Public Property Let avry(Value() As String)
ReDim pary(UBound(Value)) As String
For i = LBound(Value) To UBound(Value)
pary(i) = Value(i)
Next i
End Property
Public Property Get field() As Long
field = pfield
End Property
Public Property Let field(Value As Long)
pfield = Value
End Property
Private Sub Class_Initialize()
pfield = 0
End Sub
This worked for me
Sub test()
Dim Arr(2) As String
Arr(0) = ""
Arr(1) = "Pizza"
Arr(2) = "Hoes"
Dim obj As Cats
Set obj = New Cats
obj.avry = Arr
obj.field = 4
MsgBox obj.ary(2)
End Sub
Public Property Get ary(ByVal index As Long) As String
ary = pary(index)
End Property
Public Property Let avry(vValue As Variant)
ReDim pary(UBound(vValue)) As String
Dim i As Long
For i = LBound(vValue) To UBound(vValue)
pary(i) = vValue(i)
Next i
End Property
Public Property Get field() As Long
field = pfield
End Property
Public Property Let field(Value As Long)
pfield = Value
End Property
Private Sub Class_Initialize()
pfield = 0
End Sub
As Tim said, you can pass the array as a variant. Your MsgBox is trying to find a UBound of a String data type, so that was a problem. Also, you weren't passing an argument to ary in the MsgBox. The ary property returns a String, but you were using the Set keyword, which was another problem.
There seems to be an issue with passing array parameters to class properties.
You can get around this by switching the Let parameter to a Variant:
Public Property Let avry(ByRef arrVal As Variant)
Dim i As Integer
If IsArray(arrVal) Then
ReDim pary(LBound(arrVal) To UBound(arrVal))
For i = LBound(arrVal) To UBound(arrVal)
pary(i) = arrVal(i)
Next i
End If
End Property

VBA - Returning array from Property Get

If arrays are returned by reference, why doesn't the following work:
'Class1 class module
Private v() As Double
Public Property Get Vec() As Double()
Vec = v()
End Property
Private Sub Class_Initialize()
ReDim v(0 To 3)
End Sub
' end class module
Sub Test1()
Dim c As Class1
Set c = New Class1
Debug.Print c.Vec()(1) ' prints 0 as expected
c.Vec()(1) = 5.6
Debug.Print c.Vec()(1) ' still prints 0
End Sub
You don't have a let property. Also, the get property is returning the entire array, rather than just the element in question. Change the return type of Property Get from Double() to just plain Double. Add Property Let. Note that it takes two inputs, but only one is passed to it. The last variable (MyValue, in this case) is assumed to get it's value from whatever is after the = sign. Put a break point somewhere early in Test1() and see how the values are affected in the Locals window. Compare the variables created by the original code versus my code:
'Class1 class module
Private v() As Double
Public Property Get Vec(index As Long) As Double
Vec = v(index)
End Property
Public Property Let Vec(index As Long, MyValue As Double)
v(index) = MyValue
End Property
Private Sub Class_Initialize()
ReDim v(0 To 3)
End Sub
' end class module
'Begin module
Sub Test1()
Dim c As Class1
Set c = New Class1
Debug.Print c.Vec(1) ' prints 0 as expected
c.Vec(1) = 5.6
Debug.Print c.Vec(1) ' prints 5.6
End Sub
'End module
In VBA, arrays are never returned by reference unless they are returned through a ByRef parameter. Furthermore, whenever you use = to assign an array to a variable, you've made a new copy of the array, even if you're assigning it to a ByRef argument inside of a procedure, so you're pretty much out of luck trying to make this work.
Some alternative are...
Use a VBA.Collection instead of an array.
Make your own class that encapsulates an array and exposes procedures for indirectly accessing and manipulating the internal array.
I want to suggest another nice way to do this using a Collection and a static Property without the need to use a class:
imagine you want to have the xlCVError enum as an array (or collection), e.g. to loop through it on errors and handle it based on the actual error.
The following is initialized once on access:
'from https://stackoverflow.com/a/56646199/1915920
Static Property Get XlCVErrorColl() As Collection
Dim c As Collection 'will be already initalized after 1st access
'because of "Static Property" above!
Set XlCVErrorColl = c
If Not c Is Nothing Then Exit Property
'initialize once:
Set c = New Collection
c.Add XlCVError.xlErrDiv0
c.Add XlCVError.xlErrNA
c.Add XlCVError.xlErrName
c.Add XlCVError.xlErrNull
c.Add XlCVError.xlErrNum
c.Add XlCVError.xlErrRef
c.Add XlCVError.xlErrValue
Set XlCVErrorColl = c
End Property
Turning this into an array or implementing it as an array is straight forward, but collections seem to be more useful to me, with the disadvantage that their elements are not implicitely typed/(compile-time-)type checked.
So this would e.g. turn it into an (read-only) array (with the in-mem-copy-disadvantage mentioned in other answers/comments):
'from https://stackoverflow.com/a/56646199/1915920
Static Property Get XlCVErrorArr() As XlCVError()
Dim a() As XlCVError
XlCVErrorArr = a
If UBound( a ) > 0 Then Exit Property
'initialize once:
Dim c As Collection: Set c = XlCVErrorColl
ReDim a(c.Count)
Dim i As Integer: For i = 1 To c.Count
a(i) = c(i)
Next i
XlCVErrorArr = a
End Function
So transforming the example from Clayton Ss answer into a static, modifiable module property using some array it would be:
'module (no class required)
'from https://stackoverflow.com/a/56646199/1915920
Private v() As Double
Static Property Get Vec(index As Long) As Double
If UBound(v) < 3 Then 'initialize once:
ReDim v(0 To 3) 'one could initialize it with anyting after here too
end if
Vec = v(index)
End Property
Public Property Let Vec(index As Long, MyValue As Double)
v(index) = MyValue
End Property

Resources