Accessing Private Properties

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

How do I access a private property of a class from outside the class. I have
a class that has a property which I want to access in my code for read/write
but allow end users to only read.
 
This is one annoying limitation in VB.NET - the Getter and Setter must have
the same access modifier. This wasn't the case in VB6. However, this has
been resolved in VB2005 - you can have different access modifiers for the
Getter and Setter of the property. For the time being, you'll have to resort
to something like this:

Private Field As Integer

Public ReadOnly Property ReadOnlyProp() As Integer
Get
Return Field
End Get
End Property

Private Sub SetReadOnlyProp(ByVal i As Integer)
Field = i
End Sub

You can change the access modifier of the SetReadOnlyProp method to Friend
depending on whether you want to be able to write from within your class
only or from other classes within the project as well.


hope that helps..
Imran.
 
Dennis,

This is confusing,

I have a class that has a property which I want to access in my code for
read/write
but allow end users to only read.

Where is the code which need to access the property and who are the
endusers?

Cor
 
Thanks for answers. I resorted to using two properties, one public readonly
and the other Friend for readWrite. I thought I had seen an example on this
news group using reflection to write to private proerties. Thanks again.
 
Dennis,

Yes - you can do it via Reflection as well although it would be a little
slower. Here's how:

Public Class Testclass
Private Field As Integer = 1

Private Property TestProp() As Integer
Get
Return Field
End Get
Set(ByVal Value As Integer)
Field = Value
End Set
End Property
End Class

Private Sub TestMethod()
Dim o As New Testclass
o.GetType.GetProperty("TestProp", _
Reflection.BindingFlags.Instance Or _
Reflection.BindingFlags.NonPublic).SetValue(o, 3, Nothing)
End Sub

However, what you've come up with is also a decent solution to this problem.
You can choose either of these methods.

hope that helps..
Imran.
 
Thanks Imran, that's what I was looking for.

Imran Koradia said:
Dennis,

Yes - you can do it via Reflection as well although it would be a little
slower. Here's how:

Public Class Testclass
Private Field As Integer = 1

Private Property TestProp() As Integer
Get
Return Field
End Get
Set(ByVal Value As Integer)
Field = Value
End Set
End Property
End Class

Private Sub TestMethod()
Dim o As New Testclass
o.GetType.GetProperty("TestProp", _
Reflection.BindingFlags.Instance Or _
Reflection.BindingFlags.NonPublic).SetValue(o, 3, Nothing)
End Sub

However, what you've come up with is also a decent solution to this problem.
You can choose either of these methods.

hope that helps..
Imran.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top