What is Readable class interface

I just noticed Readable
What is the readable class interface?
Is TextInputStream part of it?
How does someone implement it?

The Readable interface defines two methods, Read(Count As Integer, Encoding As TextEncoding) As String and EndOfFile() As Boolean. Any class that has these methods, and declares itself as implementing the Readable interface, can be referred to as a Readable instead of its actual datatype. This allows you to interact with different classes without having to know exactly what type they are.

Built-in classes that implement the Readable interface include the TextInputStream, TCPSocket, IPCSocket, BinaryStream, and more.


Dim stream As Readable
If someCondition = True Then
   stream = TextInputStream.Open(someFile)
   stream = BinaryStream.Open(someFile)
End If

Dim s As String = stream.Read(1024) ' call the Readable.Read() method

Or to be a little different:
You can now check to see if a certain class has an interface implemented like so:

If TheClass IsA Readable Then
// Do reading .. 
End If

If TheClass IsA Writable Then
// Do writing.. 
Readable(TheClass).Write("Hello World")
End If

It acts like it’s a class or type (as well as it’s own type) but it’s only defining what methods with what parameters it has not how the code inside the methods is done.

There are some examples showing how this works that ship with the xojo installation.

So. I don’t use classes for reading and printing, but if I were, I’d stsrt here by checking if it had an interface. I guess it’d be very useful in networking situations.

Forum for Xojo Programming Language and IDE. Copyright © 2021 Xojo, Inc.