Accessing a Property through read-only and read-write interfaces 
Author Message
 Accessing a Property through read-only and read-write interfaces

I would like to create an object with some Property and provide two
interfaces to the object, one read-only, the other read-write.  The
compiler can then detect when I erroneously attempt to modify the
object through its read-only interface:

    Module Console

        Public Sub Main()

            Dim o As MyObject = New MyObject()

            Dim rw As IReadwrite = o
            Dim ro As IReadonly = o

            Dim value As String

            value = rw.value
            rw.value = value

            value = ro.value
            ro.value = value     ' Expected compile error

        End Sub

    End Module

In C#, I can do this:

    public interface IReadonly
    {
        string value { get; }
    }

    public interface IReadwrite
    {
        string value { get; set; }
    }

    public class MyObject : IReadonly, IReadwrite
    {
        private string _value = "some value" ;

        public string value
        {
            get { return _value ; }
            set { _value = value ; }
        }
    }

How would I code IReadonly, IReadwrite, and MyObject in VB.NET?

Thanks,
Michael.



Wed, 28 Jul 2004 23:56:31 GMT  
 Accessing a Property through read-only and read-write interfaces
Michael,
Because of the how Implements work in VB.NET I would suggest something like:

Public Interface IReadOnly
    ReadOnly Property Value() As String
End Interface

Public Interface IReadWrite
    Property Value() As String
End Interface

Public Class MyObject
    Implements IReadOnly
    Implements IReadWrite

    Private ReadOnly Property Value2() As String Implements IReadOnly.Value
        Get
            return Value
        End Get
    End Property

    Public Property Value() As String Implements IReadWrite.Value
        Get
            m_value
        End Get
        Set(ByVal Value As String)
            m_value = value
        End Set
    End Property

End Class

Hope this helps
Jay


Quote:
> I would like to create an object with some Property and provide two
> interfaces to the object, one read-only, the other read-write.  The
> compiler can then detect when I erroneously attempt to modify the
> object through its read-only interface:

>     Module Console

>         Public Sub Main()

>             Dim o As MyObject = New MyObject()

>             Dim rw As IReadwrite = o
>             Dim ro As IReadonly = o

>             Dim value As String

>             value = rw.value
>             rw.value = value

>             value = ro.value
>             ro.value = value     ' Expected compile error

>         End Sub

>     End Module

> In C#, I can do this:

>     public interface IReadonly
>     {
>         string value { get; }
>     }

>     public interface IReadwrite
>     {
>         string value { get; set; }
>     }

>     public class MyObject : IReadonly, IReadwrite
>     {
>         private string _value = "some value" ;

>         public string value
>         {
>             get { return _value ; }
>             set { _value = value ; }
>         }
>     }

> How would I code IReadonly, IReadwrite, and MyObject in VB.NET?

> Thanks,
> Michael.



Thu, 29 Jul 2004 06:28:03 GMT  
 Accessing a Property through read-only and read-write interfaces
Jay,

Thanks for the quick reply and your solution.  This is a great help.

Michael.


Quote:
> Michael,
> Because of the how Implements work in VB.NET I would suggest something like:

> Public Interface IReadOnly
>     ReadOnly Property Value() As String
> End Interface

> Public Interface IReadWrite
>     Property Value() As String
> End Interface

> Public Class MyObject
>     Implements IReadOnly
>     Implements IReadWrite

>     Private ReadOnly Property Value2() As String Implements IReadOnly.Value
>         Get
>             return Value
>         End Get
>     End Property

>     Public Property Value() As String Implements IReadWrite.Value
>         Get
>             m_value
>         End Get
>         Set(ByVal Value As String)
>             m_value = value
>         End Set
>     End Property

> End Class

> Hope this helps
> Jay



> > I would like to create an object with some Property and provide two
> > interfaces to the object, one read-only, the other read-write.  The
> > compiler can then detect when I erroneously attempt to modify the
> > object through its read-only interface:

> >     Module Console

> >         Public Sub Main()

> >             Dim o As MyObject = New MyObject()

> >             Dim rw As IReadwrite = o
> >             Dim ro As IReadonly = o

> >             Dim value As String

> >             value = rw.value
> >             rw.value = value

> >             value = ro.value
> >             ro.value = value     ' Expected compile error

> >         End Sub

> >     End Module

> > In C#, I can do this:

> >     public interface IReadonly
> >     {
> >         string value { get; }
> >     }

> >     public interface IReadwrite
> >     {
> >         string value { get; set; }
> >     }

> >     public class MyObject : IReadonly, IReadwrite
> >     {
> >         private string _value = "some value" ;

> >         public string value
> >         {
> >             get { return _value ; }
> >             set { _value = value ; }
> >         }
> >     }

> > How would I code IReadonly, IReadwrite, and MyObject in VB.NET?

> > Thanks,
> > Michael.



Thu, 29 Jul 2004 11:13:43 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Need Folder permissions Read Only / Program Access Read Write

2. File Properties - Summary Tab - Read and Write Properties

3. VB -- Long File Name , Fast Binary File Read / Write , interface with DLL

4. read write error when attempting to write to dbo_tblCounts (Access linked table)

5. Converting returned of a select query to read/write from read-only

6. EXCEL: Switching from READ-ONLY to READ-WRITE

7. Text box: read only/read write, how?

8. Reading Access 2.0 db on network w/read-only access

9. Designer tries to write to read-only VisibleColumnCount property of DataGrid

10. How: Public Read Only / Private Write Property?

11. Read-only and Write-only Properties

12. Read/Write INI Files w/Access 7.0

 

 
Powered by phpBB® Forum Software