Subclassing folderitem

I use subclassing frequently in my projects. Needless to say, this is a very useful principle that keeps your code clean and compact.
Although subclassing the folderitem-class works differently. When you subclass a folderitem which is a folder (IsFolder) then it may have children. And it’s children are just folderitem objects defined by the system, it’s type is not equal to the subclassed parent.

Does anybody know how a handy way change the type of its children, equal to the subclassed parent?

If you’re using the Child() function, then you can override that and return a instance of the subclass that represents the child folderitem. As an example, let’s say you have a FolderItem subclass called “SuperFolderItem” that looks like this:

Protected Class SuperFolderItem
Inherits FolderItem

	Function Child(name As String, followAlias As Boolean = True) As SuperFolderItem
	  return new SuperFolderItem( Super.Child(name, followAlias) )
	End Function

	Sub Constructor(f as FolderItem)
	  // Calling the overridden superclass constructor.
	  Super.Constructor( f )
	End Sub

End Class

You would do the same for ChildAt, etc.

If you’re using Children() then you’d need to override the iterable. I haven’t looked in to that as I think it’d take more time than I currently have, but I suspect it’s possible.

2 Likes

This is one of this things you can’t do in Xojo: you can’t subclass a Folderitem. I believe this is explicitly mentioned in the documentation somewhere. I believe this is also true of a number of other built-in classes.

Essentially it’s because there is no way to intercept the creation of the Folderitem object and tell it to use a subclass instead. This could hypothetically be done, but Xojo would have to make the change.

Look into Extends if you need to add functionality to Folderitem.

2 Likes

Tnanks @Anthony_G_Cyphers . Nice stuff to play with.

2 Likes

Thanks @Eric_Williams