Readonly Properties and passing byRef anomaly...

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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
 
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 said:
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!
 
DareDevil said:
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
 
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
 
the variable inDirInfo on the line "corrected = Get...." is a typo and should read myDirInfo
 
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.
 
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 said:
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...
'---------------------------------------------------------------------------
'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!
 
DareDevil said:
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
 
Back
Top