Subclassing BinaryStream

Hi,

Am I right in thinking binaryStream cannot be subclassed ?

For example:

  • create a subclass myBinaryStream
  • put this code in a button:

var b as myBinaryStream
var f as FolderItem

Var inputItem As FolderItem = FolderItem.ShowOpenFileDialog(“”)

If inputItem <> Nil Then

b = myBinaryStream.Open(f, False)
b.close

End If

This will not compile in the IDE, it states:

Conclusion: It suggests when a BinaryStream is subclassed, the OPEN method returns a BinaryStream regardless, instead of a myBinaryStream.

I expected a method of the parent class can be used in the subClass (true). Except… when that method returns an object, it should be cast as the subclass, not the parentClass.

Conversely I CAN subclass FolderItem and hide all the mechanics of a binary stream inside that to do what I need. Doesn’t make a lot of sense, but whatever, if it works I’ll live with it…

You can’t subclass Folderitem either, as the methods that create Folderitems only create Folderitems, just like BinaryStream.

Consider creating an abstract class that maintains a BinaryStream as a private property and recreates all the external methods you need. Or, add a Constructor that takes a regular BinaryStream and manages it internally:

myBinaryStream=new myBinaryStream(b)

That’s not the problem. The Open method is a Shared Method on the BinaryStream class and only returns a BinaryStream. If you want one for your own class, you’ll need to make a shared method on your own class and have it return a myBinaryStream.

2 Likes

You can also add an Extend method in a module to add more methods to the BinaryStream.
In most of my projects, I need to save strings longer than 255 characters, so WritePString isn’t the ideal choice. I’m therefore using ReadLongPString and WriteLongPString methods, that use the Extend keyword on BinaryStreams.

1 Like

Thanks Arnaud, of course, I forgot about Extend. That should do very nicely :slight_smile: