471,086 Members | 1,353 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,086 software developers and data experts.

Set internal readonly property variable

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

Dec 16 '05 #1
2 3080
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
<mi***@miben.net> wrote in message
news:11*********************@g47g2000cwa.googlegro ups.com...
|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
|
Dec 16 '05 #2
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]


<mi***@miben.net> wrote in message
news:11*********************@g47g2000cwa.googlegro ups.com...
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

Dec 17 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by Sacha Faust | last post: by
2 posts views Thread by Ranier Dunno | last post: by
1 post views Thread by Jumping Matt Flash | last post: by
5 posts views Thread by Rob Meade | last post: by
2 posts views Thread by Martin Gregersen | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.