serhio
serhio

Reputation: 28586

ReadOnly in C# vs in VB.NET

It seems that VB.NET and C# readonly keyword have some differences...

Say, a ReadOnly property in C# can be assigned in some conditions, but in VB.NET - never?

Upvotes: 5

Views: 6306

Answers (2)

Pradeep Kumar
Pradeep Kumar

Reputation: 6979

In VB.NET the read-only property is usually created to be read-only from external class. If you want to set this property, you can easily do it from inside the class, by changing the realated local variable.

So, e.g. in VB 2010

Public ReadOnly Property SomeVariable() As String

or in earlier versions,

Private _SomeVariable As String
Public ReadOnly Property SomeVariable() As String
    Get
        Return _SomeVariable
    End Get
End Property

you can set it from inside your class as:

_SomeVariable = somevalue

The property value can not be modified from an external class.

Upvotes: 4

RB.
RB.

Reputation: 37222

In C#, readonly is a field modifier. It specifies that the field can be assigned to only on initialization or in the constructor.

VB.NET is the same, except that ReadOnly is also a property modifier. It specifies that the property cannot be assigned to - i.e., it is a getter.

Upvotes: 10

Related Questions