I am right saying that Xojo interfaces only support methods and notes so therefore properties cannot be part of an interface and you have to mimic the behaviour of properties through methods? I just thought it was a little strange so I wanted to ask why I am disallowed from doing this? It is just because that is the way it is or is there a particular reason for this limitation? Or I am just doing it wrong?
I am having no problems but I am just making sure.
An interface defines what methods a class must implement (sometimes called a contract). So an interface doesn’t contain any state - objects carry state.
But you can mimic properties in an interface by creating a getter and a setter method:
Interface ABC
Property SomeString
Function Get() As String
End Function
Sub Set(Assigns value As String)
End Sub
End Property
End Interface
Of course you will need to create a private property (probably called mSomeString As String) in each of the classes which implements the above interface - which is inelegant. So if you need state, you should create an abstract class instead.
[quote=121190:@Eli Ott]An interface defines what methods a class must implement (sometimes called a contract). So an interface doesn’t contain any state - objects carry state.
But you can mimic properties in an interface by creating a getter and a setter method:
Interface ABC
Property SomeString
Function Get() As String
End Function
Sub Set(Assigns value As String)
End Sub
End Property
End Interface
Of course you will need to create a private property (probably called mSomeString As String) in each of the classes which implements the above interface - which is inelegant. So if you need state, you should create an abstract class instead.[/quote]
Thanks. Your technique is exactly the same as mine so thanks for the clarification.
[quote=121190:@Eli Ott]An interface defines what methods a class must implement (sometimes called a contract). So an interface doesn’t contain any state - objects carry state.
But you can mimic properties in an interface by creating a getter and a setter method:
Interface ABC
Property SomeString
Function Get() As String
End Function
Sub Set(Assigns value As String)
End Sub
End Property
End Interface
Of course you will need to create a private property (probably called mSomeString As String) in each of the classes which implements the above interface - which is inelegant. So if you need state, you should create an abstract class instead.[/quote]
I do not think abstract classes exist in Xojo? I do not understand what you mean by state - is it a programming term? Thanks
Yes, state is a programming term. You have behavior (code) and data (state) in an object. An Interface describes the necessary behavior required of a class, at least partially, while a class implements that behavior and stores state (data).
For example:
Class Person
Public Name As String
Sub SayHello()
Print "Hello, " + Name
End Sub
End Class
You could have 5 different Person classes, all with a different “state”, i.e. one is John, Joe, Jack, Jim and Jennifer. They all have the same behavior… one can set the name, get the name and say hello to the name.
[quote=121205:@Jeremy Cowgar]Yes, state is a programming term. You have behavior (code) and data (state) in an object. An Interface describes the necessary behavior required of a class, at least partially, while a class implements that behavior and stores state (data).
For example:
Class Person
Public Name As String
Sub SayHello()
Print "Hello, " + Name
End Sub
End Class
You could have 5 different Person classes, all with a different “state”, i.e. one is John, Joe, Jack, Jim and Jennifer. They all have the same behavior… one can set the name, get the name and say hello to the name.
[quote=121256:@Oliver Scott-Brown]Thanks. Although I already had an understanding of interfaces vs classes, this explanation good come in handy I was to teach somebody how to program.
Good and simple explanation![/quote]
I was answering your question about State, not really interfaces and classes Guess it got lost in the verbosity.