Using properties in interface?

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.

Thanks

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.

A good general read is: http://en.wikipedia.org/wiki/Object_(computer_science)

You cannot create instances of an abstract class, so in Xojo you create the standard constructor and set its scope to Private.

State = values of variables at a certain point in time (for example the values of all properties in one instance of a class).

[quote=121209:@Eli Ott]You cannot create instances of an abstract class, so in Xojo you create the standard constructor and set its scope to Private.

State = values of variables at a certain point in time (for example the values of all properties in one instance of a class).[/quote]
Okay, thanks.

[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.

A good general read is: http://en.wikipedia.org/wiki/Object_(computer_science)[/quote]
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=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 :slight_smile: Guess it got lost in the verbosity.

I think you explained it very well. I think your explanation does show the relevance of state with interfaces and classes.

Thanks