473,320 Members | 1,861 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,320 software developers and data experts.

Readonly Properties and passing byRef anomaly...

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 the user
It then dawned on me that I was passing in a readonly property into the method yet neither at compile time or runtime was I getting any kind of error or warning.

I tested with a simple string field and it alter the string as expected but with a readonly property when it leaves the method the injected property remains unaffected but surprisingly no error is generated to indicate that an attempt have been made to assign a new value to a readonly property

Maybe the compiler should smell a rat with a readonly property being passed into a method byRef, I don't know, but certainly I would expect a runtime error of some kind. Has anyone else encounterd this 'feature'

'code snippit..
dim corrected as Boolea
Dim myDirInfo As IO.DirectoryInfo = New IO.DirectoryInfo( "???" ) 'pathname name of a non existant folder
corrected = GetFolderPath(inDirInfo.FullName) 'as expected fullname doesn't change but no kind of error is raised either

'method code..
Public Function GetFolderPath(ByRef inSelectedPath As String) As Boolea
Dim dialog As New Windows.Forms.FolderBrowserDialo
Dim dr As DialogResul
Dim okayed As Boolean = Fals
dialog.Description = "The following import folder does not exist: " & vbCrLf & inSelectedPath & vbCrLf & vbCrLf & "Please select a valid folder path...
Dim folderPart As String = inSelectedPat
While Not System.IO.Directory.Exists(folderPart) AndAlso folderPart <> "
folderPart = IO.Directory.GetParent(folderPart).FullNam
End Whil
dialog.ShowNewFolderButton = Fals
dialog.SelectedPath = folderPar
okayed = dialog.ShowDialog() = DialogResult.O
If okayed The
'-----------------------
inSelectedPath = dialog.SelectedPath 'this is the assignment that should cause problems either here or upon the method return, but it doesn't
'-----------------------
End I
dialog.Dispose(
Return okaye
End Function
Nov 20 '05 #1
7 2452
pff its not very clear in that chunk of code :/

but i didn't seem to find the declaration and the type of inSelectedPath ,
if that is acting up we'll need more info on that (and pls try to structure
the code a bit, its a lot easyer on the eye ;))

eric
"DareDevil" <an*******@discussions.microsoft.com> wrote in message
news:8C**********************************@microsof t.com...
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 the user. It then dawned on me that I was passing in a readonly property into the method yet neither at compile time or runtime was I getting any kind of
error or warning.
I tested with a simple string field and it alter the string as expected but with a readonly property when it leaves the method the injected property
remains unaffected but surprisingly no error is generated to indicate that
an attempt have been made to assign a new value to a readonly property.
Maybe the compiler should smell a rat with a readonly property being passed into a method byRef, I don't know, but certainly I would expect a
runtime error of some kind. Has anyone else encounterd this 'feature'?
'code snippit...
dim corrected as Boolean
Dim myDirInfo As IO.DirectoryInfo = New IO.DirectoryInfo( "???" ) 'pathname name of a non existant folder! corrected = GetFolderPath(inDirInfo.FullName) 'as expected fullname doesn't change but no kind of error is raised either!

'method code...
Public Function GetFolderPath(ByRef inSelectedPath As String) As Boolean
Dim dialog As New Windows.Forms.FolderBrowserDialog
Dim dr As DialogResult
Dim okayed As Boolean = False
dialog.Description = "The following import folder does not exist: " & vbCrLf & inSelectedPath & vbCrLf & vbCrLf & "Please select a valid folder
path..." Dim folderPart As String = inSelectedPath
While Not System.IO.Directory.Exists(folderPart) AndAlso folderPart <> ""
folderPart = IO.Directory.GetParent(folderPart).FullName
End While
dialog.ShowNewFolderButton = False
dialog.SelectedPath = folderPart
okayed = dialog.ShowDialog() = DialogResult.OK
If okayed Then
'------------------------
inSelectedPath = dialog.SelectedPath 'this is the assignment that should cause problems either here or upon the method return, but it doesn't! '------------------------
End If
dialog.Dispose()
Return okayed
End Function

Nov 20 '05 #2
"DareDevil" <an*******@discussions.microsoft.com> schrieb
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 the
user. It then dawned on me that I was passing in a readonly property
into the method yet neither at compile time or runtime was I getting
any kind of error or warning.

I tested with a simple string field and it alter the string as
expected but with a readonly property when it leaves the method the
injected property remains unaffected but surprisingly no error is
generated to indicate that an attempt have been made to assign a new
value to a readonly property.

Maybe the compiler should smell a rat with a readonly property being
passed into a method byRef, I don't know, but certainly I would
expect a runtime error of some kind. Has anyone else encounterd this
'feature'?

'code snippit...
dim corrected as Boolean
Dim myDirInfo As IO.DirectoryInfo = New IO.DirectoryInfo( "???" )
'pathname name of a non existant folder! corrected =
GetFolderPath(inDirInfo.FullName) 'as expected fullname doesn't
change but no kind of error is raised either!
'method code...
Public Function GetFolderPath(ByRef inSelectedPath As String) As
Boolean

The Fullname property is retrieved and stored in a temporary local variable
on the stack. A reference to the variable is passed to GetFolderPath. If you
change the argument within GetFolderPath, the /retrieved value/ of the
calling sub is changed, not the property. In other words: Only the return
value of inDirInfo.FullName is changed, not inDirInfo.FullName itself.

--
Armin

How to quote and why:
http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html

Nov 20 '05 #3
I hope this is a little more structured. The tabs from the pasted source code were huge so I chose to strip them out

I suspected the mechanics of the CLR were working on a copy of the string but would have expected either 1) a compile time warning that passing a readonly property into a method using byref raises the problem of how to set any changes when the method finishes or 2) a runtime error when returning from the method because the property cannot be set to reflect the value of the altered parameter. I wasn't expecting the responsibility of checking that any property being passed by ref actually having a setter would be placed wholly on the developer without any compile time or runtime support

'code snippit..
'---------------
...
dim corrected as Boolea
Dim myDirInfo As IO.DirectoryInfo = New IO.DirectoryInfo( "???" ) 'pathname name of a non existant folder
corrected = GetFolderPath(inDirInfo.FullName) 'as expected fullname doesn't change but no kind of error is raised either
...

'method code..
'-----------------------------------------------------------------------------------
Public Function GetFolderPath(ByRef inSelectedPath As String) As Boolean 'this is where the result of the property getter might be injected
'-----------------------------------------------------------------------------------
Dim dialog As New Windows.Forms.FolderBrowserDialo
Dim dr As DialogResult 'is it okayed or cancelled
Dim okayed As Boolean = False 'used to assign a value to the return keyword
Dim folderPart As String = inSelectedPath 'the pathname that get chopped up until its either blank or is an existing path
dialog.Description = "The following import folder does not exist: " & vbCrLf & inSelectedPath & vbCrLf & vbCrLf & "Please select a valid folder path...
While Not System.IO.Directory.Exists(folderPart) AndAlso folderPart <> "" 'try to salvage a some sort of path that exist
folderPart = IO.Directory.GetParent(folderPart).FullNam
End Whil
'now do the user dialogue part..
dialog.ShowNewFolderButton = Fals
dialog.SelectedPath = folderPar
okayed = dialog.ShowDialog() = DialogResult.O
If okayed The
'-----------------------
inSelectedPath = dialog.SelectedPath 'this is the assignment that should cause problems either here or upon the method return, but it doesn't
'-----------------------
End I
dialog.Dispose(
Return okayed 'now the function is being exited any relevent property setters would be called
End Function
Nov 20 '05 #4
the variable inDirInfo on the line "corrected = Get...." is a typo and should read myDirInfo
Nov 20 '05 #5
I've had a look in C# and it appears you cannot pass a property using 'ref' even if it has a getter and a setter so VB.NET is a bit more flexible in that respect. In VB.NETif the property has a setter too then it all works as expected i.e. the setter is invoked once the method ends with the final value of the relevant parameter, its just that it offers no indication that any change will be lost in the ether for properties that have no setter.
Nov 20 '05 #6
Ok i c it now, Armin has already explained wats happening so i won't go into
that.
Should it be possible to do this without error (like you i don't c a use for
it, but there could be)
corrected = GetFolderPath(inDirInfo.FullName) 'as expected fullname doesn't change but no kind of error is raised either!

I would c this as bad coding, while using the function the programmer
clearly sees the ByRef and should know better that to assign a readonly to
it.

to completely avoid this you would have to create your function to return
the string instaid of a boolean, using ByRef will always! leave this
possibility.
Public Function GetFolderPath(ByVal inSelectedPath As String) As String

If you absolutely need your boolean you can create your own output class
that has a string and a boolean in it
if you create the boolean as default property (not 100% sure bout the
default keyword) it will return that as you reference to the class without
..parameter and consequently it wil also work if you use the function to test
if your path is valid ...

I know it's not much help but i'm afraid there isn't much to help.

eric
"DareDevil" <an*******@discussions.microsoft.com> wrote in message
news:AE**********************************@microsof t.com... I hope this is a little more structured. The tabs from the pasted source code were huge so I chose to strip them out.
I suspected the mechanics of the CLR were working on a copy of the string but would have expected either 1) a compile time warning that passing a
readonly property into a method using byref raises the problem of how to set
any changes when the method finishes or 2) a runtime error when returning
from the method because the property cannot be set to reflect the value of
the altered parameter. I wasn't expecting the responsibility of checking
that any property being passed by ref actually having a setter would be
placed wholly on the developer without any compile time or runtime support.
'code snippit...
'----------------
...
dim corrected as Boolean
Dim myDirInfo As IO.DirectoryInfo = New IO.DirectoryInfo( "???" ) 'pathname name of a non existant folder! ...

'method code...
'---------------------------------------------------------------------------
--------- Public Function GetFolderPath(ByRef inSelectedPath As String) As Boolean 'this is where the result of the property getter might be injected! '---------------------------------------------------------------------------
--------- Dim dialog As New Windows.Forms.FolderBrowserDialog
Dim dr As DialogResult 'is it okayed or cancelled.
Dim okayed As Boolean = False 'used to assign a value to the return keyword. Dim folderPart As String = inSelectedPath 'the pathname that get chopped up until its either blank or is an existing path. dialog.Description = "The following import folder does not exist: " & vbCrLf & inSelectedPath & vbCrLf & vbCrLf & "Please select a valid folder
path..." While Not System.IO.Directory.Exists(folderPart) AndAlso folderPart <> "" 'try to salvage a some sort of path that exist. folderPart = IO.Directory.GetParent(folderPart).FullName
End While
'now do the user dialogue part...
dialog.ShowNewFolderButton = False
dialog.SelectedPath = folderPart
okayed = dialog.ShowDialog() = DialogResult.OK
If okayed Then
'------------------------
inSelectedPath = dialog.SelectedPath 'this is the assignment that should cause problems either here or upon the method return, but it doesn't! '------------------------
End If
dialog.Dispose()
Return okayed 'now the function is being exited any relevent property setters would be called. End Function

Nov 20 '05 #7
"DareDevil" <an*******@discussions.microsoft.com> schrieb
I've had a look in C# and it appears you cannot pass a property using
'ref' even if it has a getter and a setter so VB.NET is a bit more
flexible in that respect. In VB.NETif the property has a setter too
then it all works as expected i.e. the setter is invoked once the
method ends with the final value of the relevant parameter, its just
that it offers no indication that any change will be lost in the
ether for properties that have no setter.


You're right. I wanted to write an explanation as a reply to your other
message but during my investingations I found out that my explanation would
have been wrong:

"You don't pass a property to the procedure. You only pass the return value
of a function to the procedure. It doesn't matter whether the return value
that is passed to the next procedure came from a Readonly property. The
property itself isn't affected anyway."

But as you said, what really happens is something like like the following,
so the last sentence would have been wrong.

dim tmp as string
tmp = object.property
MySub(tmp) 'Byref argument
object.property = tmp 'skipped for readonly properties

I didn't know this. I thought that the only thing that is changed within
MySub is the return value of the property, not the property itself.

Now I agree that a warning should be shown whenever the property is
readonly.

--
Armin

How to quote and why:
http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html
Nov 20 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Andy Read | last post by:
Dear all, I thought I understood passing parameters ByVal and ByRef but I clearly don't! If I define a simple class of: Public Class Person Public Name as String Public Age as Integer End...
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...
5
by: Onno Ceelen | last post by:
Hi, I am testing interop code by use of some prototypes. What I have is the following: 1. VB 6.0 library 2. VB.NET class library 3. Interop assembly of the VB 6.0 library (tlbimp output) ...
2
by: Neil Munro | last post by:
I'm having some "type" difficulty in passing a .NET array (byref) to a COM based API (Autodesk Inventor). In VB6 the code to return an array of tolerance values is: Dim ToleranceCount As Long...
4
by: Erik Foreman | last post by:
this is what I have 'variables defined as arrays Dim ceday(), ceti(), ceto(), ceproj(), cenotes() As String Dim cerow As Int32
2
by: Witold Iwaniec via .NET 247 | last post by:
It seems that when you pass an object to a function it is always passed by reference even if it is explicitly declared ByVal. Is it the behavior of VB.Net? Here is sample code from sample Asp.Net...
11
by: John Pass | last post by:
Hi, In the attached example, I do understand that the references are not changed if an array is passed by Val. What I do not understand is the result of line 99 (If one can find this by line...
12
by: eric.goforth | last post by:
Is there any reason to use: Private newPropertyValue As Integer Public ReadOnly Property MyProperty(ByRef MyParam as Integer) As Integer Get Return newPropertyValue End Get End Property
10
by: Timothy | last post by:
Hi, I was looking over some differences between C# and VB code today and noticed that the set method in properties act completely different. C# values are passed by reference, and VB values are...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.