Set internal readonly property variable

M

miben

I need to set a variable returned by a readonly property in a class by
another class. So the only way to set that value is from a specific
class and function.

Public Sub Main
Dim setter As New GoingToSetYou
Dim item As New ToBeSet

setter.NewItem(item)
Dim sValue As String = item.Value
End Sub

Public Class GoingToSetYou
Public Function NewItem(item As ToBeSet)

I want to set the private variable m_sValue here some how and
keep the access of it away from others

End Function
End Class

Public Class ToBeSet
Private m_sValue As String

Public ReadOnly Property Value As String
Return m_sValue
End Property
End Class
 
J

Jay B. Harlow [MVP - Outlook]

Miben,
|I need to set a variable returned by a readonly property in a class by
| another class. So the only way to set that value is from a specific
| class and function.
You can use a Friend method/member, as long as both types are within a
single Assembly (executable or Class Library). Types outside that Assembly
will not be able to directly access the Friend member. However any type
within the first Assembly will have direct access to the Friend member.


In VB 2002 & 2003 I normally create a SetValue method for the Value
property, something like:

| Public Class ToBeSet
| Private m_sValue As String
|
| Public ReadOnly Property Value As String
Get
| Return m_sValue
End Get
| End Property

Friend Sub SetValue(value As String)
m_sValue = value
End Sub

| End Class

In VB 2005 you can simply define the Property Setter as a different access
level:

| Public Class ToBeSet
| Private m_sValue As String
|
| Public Property Value As String
Get
| Return m_sValue
End Get
Friend Set(value As String)
m_sValue = value
End Set
| End Property

| End Class

I would consider continuing to use the Sub SetValue method in VB 2005 if I
wanted or needed the property to be Readonly...

--
Hope this helps
Jay [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net


|I need to set a variable returned by a readonly property in a class by
| another class. So the only way to set that value is from a specific
| class and function.
|
| Public Sub Main
| Dim setter As New GoingToSetYou
| Dim item As New ToBeSet
|
| setter.NewItem(item)
| Dim sValue As String = item.Value
| End Sub
|
| Public Class GoingToSetYou
| Public Function NewItem(item As ToBeSet)
|
| I want to set the private variable m_sValue here some how and
| keep the access of it away from others
|
| End Function
| End Class
|
| Public Class ToBeSet
| Private m_sValue As String
|
| Public ReadOnly Property Value As String
| Return m_sValue
| End Property
| End Class
|
 
M

m.posseth

hello


i believe the safest way of setting the initial value of a readonly property
is by setting it through the constructor of the class
this way it would externall behave like a constant without a way of setting
it to another value ( only possibility is to initiate ( constuct ) the
complete object again )

example of this aproach ::

Friend Class example

Private _propExample As String

Friend ReadOnly Property propExample() As String

Get

Return _propExample

End Get

End Property

Public Sub New(ByVal setProp As String)

_propExample = setProp

End Sub

End Class

i hope you get the idea


regards

Michel Posseth [MCP]
 

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

Top