By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,156 Members | 962 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,156 IT Pros & Developers. It's quick & easy.

Set internal readonly property variable

P: n/a
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
Share this Question
Share on Google+
2 Replies


P: n/a
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

P: n/a
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.