PDA

View Full Version : improving class modules with syntax sugar



sanderlander
05-28-2012, 03:19 PM
This is my first post, and I'm happy to be here!

I created a class module that holds a single string array. The purpose of the module is to provide convenience functions to access the array. For example, my_sarray.append("dog") will add "dog" to the end of my_sarray. I access the array elements with my_sarray.get(index). These functions work well.

What I would like to do is use my_sarray(0) to grab the first element in the array. I would also like to use "for each item in my_sarray" to iterate through the array elements. Is that possible?

Bob Phillips
05-28-2012, 03:47 PM
Surely, my_sarray.get(0) grabs the first element of the array.

For Each Item might be harder to achieve, as this normally is used to iterate each item in a collection, although your class has multiple values it is still just a single object, so there is nothing to iterate through. You could maybe add a GetNext function and a CurrentIndex property and do something like

myVal = my_sarray.Get(0)
Do While my_sarray.CurrentIndex >= 0

'proces myVal
myVal = my_sarray.GetNext
Loop

This would allow starting anywhere within the array.

mikerickson
05-28-2012, 05:58 PM
You could have the property Get to accept an optional argument. If the argument is present, it returns the single index (the argument), if the argument is omitted, then it returns the whole array.

Dim myArray As Variant

Property Get gGet(Optional index As Variant) As Variant
If IsMissing(index) Then
gGet = myArray
Else
gGet = myArray(index)
End If
End Property


The next step to acquiring the syntax you want would be to have Get the default property.
http://www.cpearson.com/excel/DefaultMember.aspx

Then, with gGet as the default property
For each oneThing in my_sArray
' would be read as
For Each oneThing in my_sArray.Get

Also, I'm curious. What code do you currently use for your Get. My excel throws a compile error with
Property Get get() as Variant

sanderlander
05-28-2012, 06:59 PM
Mike:
I'll try your idea and see how it works out. As for the compile error you mention, I actually don't use the properties in my class module because I don't understand how they would help me (in general I don't understand them). Instead, I just declare my array as a "private" in the outer-most scope in the module, and then I have my "add" method add things to the array and my "get" method return items from the array.

sanderlander
05-28-2012, 07:06 PM
xld:
The solution you proposed is what I am currently using in my code. So, it's nice to see people are coming up with similar solutions. Mike's post below yours says that I should be able to use properties to enable iteration, which might be interesting. I'm going to give that a try and see what happens.

Bob Phillips
05-29-2012, 12:57 AM
Personally, I would ditch the approach and goo whole hog for a collection class. You will get a much smoother implementation f everything you are trying to do.