473,383 Members | 1,733 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 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 3183
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Sacha Faust | last post by:
if you have a ready only properly like string.Length and I do --(string.Length) I get a compiler problem saying that I can't modify the value of the readonly property. From my understanding, the...
2
by: Ranier Dunno | last post by:
Hi, I have a class hierarchy, and would like all classes in the tree to have a common, static readonly variable - with a class-specific value. I'd like it to be readonly (it should never...
0
by: Brian Young | last post by:
Hi all. I'm using the Property Grid control in a control to manage a windows service we have developed here. The windows service runs a set of other jobs that need to be managed. The control...
7
by: DareDevil | last post by:
I have written a method that should modify the folder path passed to it into one that exists and is selected by the user. It then returns a boolean depending on whether a folder path was selected by...
1
by: Jumping Matt Flash | last post by:
I'm trying to achieve a property within a class which returns a conditional result dependent on the class members values. I will explain.. Public Class Address Private addrCountry As String...
5
by: Rob Meade | last post by:
Hi all, Ok - I guess this one will eventually still end up as a "personal choice" kinda answer, but I thought I'd run it passed the wealth of knowledge and experience that's in this group. ...
2
by: Saran | last post by:
Hi, Below is my scenario... I want to restrict my clients to access one of my class property in ReadOnly mode. At the same time as an author of the component i would like to have read-write...
2
by: Martin Gregersen | last post by:
Hi Looking for a way to bind a readonly property on a textbox to a parameter, not having to cycle through all controls, but rather setting <textbox id="myTextbox" readonly="<%# SetReadOnly...
1
by: Brad Pears | last post by:
Perfect. That makes more sense then calling a method to run the Stored Proc and return the value in the read only properties "Get" method because then it would be running that everytime someone...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.