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.DirectoryInf o = New IO.DirectoryInf o( "???" ) 'pathname name of a non existant folder
corrected = GetFolderPath(i nDirInfo.FullNa me) 'as expected fullname doesn't change but no kind of error is raised either
'method code..
Public Function GetFolderPath(B yRef inSelectedPath As String) As Boolea
Dim dialog As New Windows.Forms.F olderBrowserDia lo
Dim dr As DialogResul
Dim okayed As Boolean = Fals
dialog.Descript ion = "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.Direc tory.Exists(fol derPart) AndAlso folderPart <> "
folderPart = IO.Directory.Ge tParent(folderP art).FullNam
End Whil
dialog.ShowNewF olderButton = Fals
dialog.Selected Path = folderPar
okayed = dialog.ShowDial og() = DialogResult.O
If okayed The
'-----------------------
inSelectedPath = dialog.Selected Path '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 7 2470
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*******@disc ussions.microso ft.com> wrote in message
news:8C******** *************** ***********@mic rosoft.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.DirectoryInf o = New IO.DirectoryInf o( "???" )
'pathname name of a non existant folder! corrected = GetFolderPath(i nDirInfo.FullNa me) 'as expected fullname
doesn't change but no kind of error is raised either!
'method code... Public Function GetFolderPath(B yRef inSelectedPath As String) As Boolean Dim dialog As New Windows.Forms.F olderBrowserDia log Dim dr As DialogResult Dim okayed As Boolean = False dialog.Descript ion = "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.Direc tory.Exists(fol derPart) AndAlso folderPart <> "" folderPart = IO.Directory.Ge tParent(folderP art).FullName End While dialog.ShowNewF olderButton = False dialog.Selected Path = folderPart okayed = dialog.ShowDial og() = DialogResult.OK If okayed Then '------------------------ inSelectedPath = dialog.Selected Path '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
"DareDevil" <an*******@disc ussions.microso ft.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.DirectoryInf o = New IO.DirectoryInf o( "???" ) 'pathname name of a non existant folder! corrected = GetFolderPath(i nDirInfo.FullNa me) 'as expected fullname doesn't change but no kind of error is raised either!
'method code... Public Function GetFolderPath(B yRef 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.FullN ame is changed, not inDirInfo.FullN ame 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.DirectoryInf o = New IO.DirectoryInf o( "???" ) 'pathname name of a non existant folder
corrected = GetFolderPath(i nDirInfo.FullNa me) 'as expected fullname doesn't change but no kind of error is raised either
...
'method code..
'-----------------------------------------------------------------------------------
Public Function GetFolderPath(B yRef inSelectedPath As String) As Boolean 'this is where the result of the property getter might be injected
'-----------------------------------------------------------------------------------
Dim dialog As New Windows.Forms.F olderBrowserDia lo
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.Descript ion = "The following import folder does not exist: " & vbCrLf & inSelectedPath & vbCrLf & vbCrLf & "Please select a valid folder path...
While Not System.IO.Direc tory.Exists(fol derPart) AndAlso folderPart <> "" 'try to salvage a some sort of path that exist
folderPart = IO.Directory.Ge tParent(folderP art).FullNam
End Whil
'now do the user dialogue part..
dialog.ShowNewF olderButton = Fals
dialog.Selected Path = folderPar
okayed = dialog.ShowDial og() = DialogResult.O
If okayed The
'-----------------------
inSelectedPath = dialog.Selected Path '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(i nDirInfo.FullNa me) '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(B yVal 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*******@disc ussions.microso ft.com> wrote in message
news:AE******** *************** ***********@mic rosoft.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.DirectoryInf o = New IO.DirectoryInf o( "???" )
'pathname name of a non existant folder! ...
'method code...
'---------------------------------------------------------------------------
--------- Public Function GetFolderPath(B yRef inSelectedPath As String) As Boolean
'this is where the result of the property getter might be injected!
'---------------------------------------------------------------------------
--------- Dim dialog As New Windows.Forms.F olderBrowserDia log 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.Descript ion = "The following import folder does not exist: " &
vbCrLf & inSelectedPath & vbCrLf & vbCrLf & "Please select a valid folder
path..." While Not System.IO.Direc tory.Exists(fol derPart) AndAlso folderPart <>
"" 'try to salvage a some sort of path that exist. folderPart = IO.Directory.Ge tParent(folderP art).FullName End While 'now do the user dialogue part... dialog.ShowNewF olderButton = False dialog.Selected Path = folderPart okayed = dialog.ShowDial og() = DialogResult.OK If okayed Then '------------------------ inSelectedPath = dialog.Selected Path '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
"DareDevil" <an*******@disc ussions.microso ft.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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 Class
|
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 is used to view the state of the running
jobs and schedule new jobs. The control also runs in the context of
Internet Explorer (we do this so the administrators of the jobs can always
receive the latest control). The property grid is used to...
|
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)
I want my VB.NET class library to have the exact same definition as my VB
|
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
Dim ExistingTolerances() As Double
'oSurfBody is declared and assigned in the following
Call oSurfBody.GetExistingFacetTolerances(ToleranceCount,
ExistingTolerances)
|
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
| |
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 application. The sub loadValueByVal takes the argument by value so after returning to calling method, the object should be unchanged but it is not
Public Class ITest
Private MyName As String
Public TestId As String
Public Sub New()
|
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 number) which is the last line of the
following sub routine:
' procedure modifies elements of array and assigns
' new reference (note ByVal)
Sub FirstDouble(ByVal array As Integer())
Dim i As Integer
|
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
|
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 passed by value... Am I right? Why is
there such a big difference in implementation of properties?
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |