Wrappers

In the RB Definitive Guide on Page 144, he mentions a wrapper that can be used with arrays. The functions for copy, clone, etc might be useful. I do not see a reference to it anymore. Can it still be used to take advantage of the functions? Would it have a super to make the functions available?

which edition of TDG ?
144 here is about Coercion

EDIT 1 : nm - found it in the one I have (page 127)

you cant subclass an array since they are not classes

so what you end up ding it creating a class that contains an array
and that class then provides whatever api which, in my case, is pages 128-129

EDIT 2 : https://www.oreilly.com/library/view/realbasic-tdg-2nd/0596001770/
Dont see a repository of exampels etc but the wrapper in the book wouldnt take long to recreate and you could still use this design in Xojo :slight_smile:

I’m looking at the second edition. On page 144 the heading is Array Class and it talks about wrappers and shows examples of functions that are allowed using the class. I would like to use these functions.

It states that: We cannot subclass and array, because an array is not a class. But we can make a class that has an array as a member and provides an extended interface to it. Such a class is called a wrapper.

Maybe you have to create your own functions in the wrapper class?

Yes - that the point of the wrapper

IF all you want is to extend the functions already possible on an array you CAN use a module with EXTENDS methods
You just have to be careful that the declarations match the type of array

ie/ if you wrote

     Sub doesntDoAnything( extends stringArray() as string )
     End Sub

you could not use this method with a single dimension array of integers or multidimensional arrays
or arrays of variants or …
the TYPE must match exactly before the extends will apply

Hope that makes sense ?

[quote=492504:@Norman Palardy]Yes - that the point of the wrapper

IF all you want is to extend the functions already possible on an array you CAN use a module with EXTENDS methods
You just have to be careful that the declarations match the type of array

ie/ if you wrote

     Sub doesntDoAnything( extends stringArray() as string )
     End Sub

you could not use this method with a single dimension array of integers or multidimensional arrays
or arrays of variants or …
the TYPE must match exactly before the extends will apply

Hope that makes sense ?[/quote]

Yes, that is starting to make sense. I had just never done it in a useful way. Do you find the concept useful in your programing?
Thanks Norman

For some types wrappers are one effective way to enhance them
esp when things can be subclassed - like arrays

I do tend to write a LOT of extensions as well since some types cant be wrapped in decent ways

Folderitem for instance
While you can subclass it its not useful since no framework methods will return instances of your subclassed type
So either you wrap them extensively OR you extend them (possibly both)

Apparently I just don’t get it:

If this is the method example in the RB guide to clone and array. I called Class cArray and the method Clone.
//cArray.clone:

Function clone() As cArray
var C as cArray
C = new cArray(self.ubound())
self.copyto ©
return c
End Funtion

Is not this what I enter into the method as code:

var C as cArray
C = new cArray(self.ubound())
self.copyto ©
return c

I receive these errors:
cArray.Clone, line 5
Type “cArray” has no member named “ubound”
C = new cArray(self.ubound())

cArray.Clone, line 5
This method requires fewer parameters than were passed
C = new cArray(self.ubound())

cArray.Clone, line 6
Type “cArray” has no member named “copyto”
self.copyto ©

Fair enough
Lets progress slowly then

Function clone() As cArray
   var C as cArray
   C = new cArray(self.ubound()) // <<< SELF isa cArray and so it would have to have a Ubound method 
   self.copyto (c) //  // <<< SELF isa cArray and so it would have to have a CopyTo method 
   return c
End Funtion

does your class, cArray, have those methods ?
If not thats why you get those errors (never mind “working”)

So in your cArray class add a new method, Ubound() as Integer, and CopyTo(copyInto as cArray)

they have no code in them so things wont “work” yet

Okay; two new method names have been created in the cArray class called: Ubound and CopyTo

OK so to make them DO something we’ll have to look at the cArray class itself

Because this is a “wrapper” what is it wrapping ?
In your class there should be a protected property , mArray() as String
This is where the actual data will be held
Its often also referred to as the "backing property’

It doesnt have to be an array FWIW - it could be a dictionary or just about anything else since all the cArray class is going to do is make things look like this is an array of data
In fact wrapping an array to make it be an array is … not entirely useful but its a decent learning exercise

OK since the cArray class now has a ubound method what would uBound return ?
It should return the uBound of the data it holds
In this case that would be the uBound of the protected array
So cArray.Ubound should probably just look like

Sub UBound() as Integer
   return mArray.Ubound
End Sub

make sense so far ?

Good so far.

I I entered this into Copyto and now it looks like I need a GET and SET

Sub copyTo(c as cArray) Var i,u as integer u = min(self.uBound(), c.uBound()) for i = 0 to u c.Set(i , self.get(i)) next End Sub

I see have it grows which makes me wonder if my old direct way of cloning was actually a better way.

OK So now that we have one cArray instance able to return its ubound we need the Constructor to take that information and do something useful with it
That will make this line more useful

  C = new cArray(self.ubound())

in the cArray class add a new method named

Sub Constructor( newuBound as integer)
     Redim mArray( newUbound )
 End Sub

If you dont already have another constructor you might make that one be like

Sub Constructor()
End Sub

with no code

I’m Back. I’m not sure what a constructor is going to give here. Aren’t they called first when when the New operator is generated or in this case C = new cArray(self.ubound()). Okay, I get it, you are sending the array newUbound to Redim mArray first.

The second construct is not named yet.

Why in other methods are the Sub and Function not used but in this class we do?

You don’t. That is a common construct to convey the sense of where code is in a non-IDE context like the forum. [quote=492556:@Norman Palardy]

Sub Constructor( newuBound as integer) Redim mArray( newUbound ) End Sub [/quote]
This means:
Add a method.
Set its name to “Constructor”.
Set its parameters to “newuBound as integer”.
In the code area, put “Redim mArray( newUbound )”.

Note: if you open a project in a text editor, you will see code that looks like this, but you are well advised not to edit it directly. Use the IDE.

[quote=492603:@Tim Hare]You don’t. That is a common construct to convey the sense of where code is in a non-IDE context like the forum.
This means:
Add a method.
Set its name to “Constructor”.
Set its parameters to “newuBound as integer”.
In the code area, put “Redim mArray( newUbound )”.[/quote]

So what about this?

Function get(index as Integer) as string
return self.theArray(index)
end Function

What part stays as code and what part is set as parameters? Get as string and Index as integer and what about the rest.

[quote=492556:@Norman Palardy]OK So now that we have one cArray instance able to return its ubound we need the Constructor to take that information and do something useful with it
That will make this line more useful

  C = new cArray(self.ubound())

in the cArray class add a new method named

Sub Constructor( newuBound as integer)
     Redim mArray( newUbound )
 End Sub

If you dont already have another constructor you might make that one be like

Sub Constructor()
End Sub

with no code[/quote]

I added the second constructor, Constructor() to methods and it’s name shows in like a sub menu of constructor as () and the other constructor as (newBounds as integer). Is that what I should see?

Exactly!

What should the parameters look like for this function:

Function get(index as Integer) as string
return self.theArray(index)
end Function

Maybe: (index as Integer) as string.
Not sure about that.

Indeed it is
SUB and FUNCTION in Xojo are just different ways to describe methods
SUB dont return values (they have no return type)
Functions do

So, as TIm pointed out, sometimes its just shorthand to write

 Sub Constructor()
 End Sub

as “create this method with no return value and name it constructor”