Search code examples
.netassembliespropertiesinternal

ReadOnly vs Property within Assembly Question/Conundrum


How can I make a Property "ReadOnly" outside the Assembly (DLL) for people using the DLL but still be able to populate that property from within the assembly for them to read?

For example, if I have a Transaction object that needs to populate a property in a Document object (which is a child class of the Transaction class) when something happens in the Transaction object, but I just want to developer using my DLL to only be able to read that property and not change it (it should only be changed from within the DLL itself).


Solution

  • C#

    public object MyProp {
       get { return val; }
       internal set { val = value; }
    }
    

    VB

    Public Property MyProp As Object
       Get
          Return StoredVal
       End Get
       Friend Set(ByVal value As Object) 
          StoredVal = value
       End Set
    End Property