Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read-write property in interface VB.NET

I would like to write an interface class and use it like this

public Interface IFunction
  property name as string
end interface

public class ModelFunction
   implements IFunction

  public property name as string

  public sub new()

end class

*EDIT(deleted following sentence for being noob, thanks @damien_the_unbeliever for pointing this out):But this is not possible to get because a property in an vb.net interface has to be readonly or writeonly (as far as i get it)*

I have now written this but seems a little wrong:

public Interface IFunction
  Readlonly property getName() as string
  writeonly property writeName() as string
end interface

public class ModelFunction
 implements IFunction

 ....
end class

Anyone have a better solution for this? or can help me out with properties in an Interface class. Have read some articles here on stackoverflow but none of them point me in the right direction.

like image 804
benst Avatar asked Jan 31 '13 09:01

benst


People also ask

How do you declare a readonly property in an interface?

Interface with readonly property is assignable to interface with mutable property. interface MutableValue<T> { value: T; } interface ImmutableValue<T> { readonly value: T; } let i: ImmutableValue<string> = { value: "hi" }; i.

Can we have property in interface?

An interface may define a default implementation for members, including properties. Defining a default implementation for a property in an interface is rare because interfaces may not define instance data fields.

How do you declare a property in an interface?

You just specify that there is a property with a getter and a setter, whatever they will do. In the class, you actually implement them. The shortest way to do this is using this { get; set; } syntax. The compiler will create a field and generate the getter and setter implementation for it.

Can I inherit property from class to interface?

A class implements an interface, it does not inherit it.


1 Answers

This works fine for me:

Public Class Class1
    Implements Thing

    Property Gary As Int32 Implements Thing.Gary
        Get
            Return 10
        End Get
        Set(value As Int32)

        End Set
    End Property
End Class

Public Interface Thing
    Property Gary As Int32
End Interface

There's even an example on the documentation page for Interface:

Interface IAsset
    Event ComittedChange(ByVal Success As Boolean)
    Property Division() As String 
    Function GetID() As Integer 
End Interface
like image 134
Damien_The_Unbeliever Avatar answered Sep 30 '22 16:09

Damien_The_Unbeliever