Operator overloading framework classes?


I know that operator overloading is possible. But now I want to do operator overloading on existing framework classes, is that possible?
Eg: I want to concatenate a literal string and a folderitem. But I don’t want to type .Nativepath everywhere. So I want to do this:

Dim example As String

example = "teststring" + myAwesomeFolderItem[/code]

The errormessage I receive is: [quote]Type TextLiteral has no definition of 'Operator_Add' with type FolderItem[/quote] (something like that)

How can I accomplish this? I know I could subclass FolderItem and do an 'Operator_AddRight'. But I'm looking for a way without subclassing and use the build in classes of Xojo. I was thinking about Extending, but I have no idea how I could do an extends with operator overloading.

I tried this:

[code]Public Function Operator_AddRight(Extends fi As FolderItem, leftside As String) as String
  Return leftside + fi.NativePath
End Function

But that doesn’t work.

Thank you!

Operator overload methods must be class methods; they can’t use the Extends syntax. I’m not sure it’s possible to do what you’re asking.

You could subclass folderitem and add your method there.

If you do this you may also want to add operator_convert(f as folderitem) and operator_convert() as folderitem to make it easy to deal with all the framework functions that take & return folderitems

So something like

   dim f as FolderItem = SpecialFolder.DesktopFolder

can just turn into

   dim f as MyFolderItem = SpecialFolder.DesktopFolder

and not cause you piles of extra work


Class MyFolderitem
  Public Function Operator_AddRight(leftside As String) as String
    Return leftside + fi.NativePath
  End Function

  Public Function Operator_convert() as folderitem
    return fi
  End Function

  Public Sub Operator_convert(f as folderitem)
    fi = f
  End Sub

  private fi as folderitem
End Class

makes code like

  Dim example As String
  Dim c As Class1 = SpecialFolder.Desktop

  example = "teststring" + c

still work reasonably simply

BUT with this approach I wont be surprised if you run into other impediments