473,883 Members | 1,617 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Make a Parameter CONST?

If I have a parameter that has an Object type (as opposed to something like
a string), can I make that parameter a CONST?

Right now, if you pass an object into a sub/function, that sub/function can
modify the object no matter how it's defined (ByVal or ByRef).

In some cases, I want to make sure that you cannot modify the object. Is
there a way to do that in VS2003 or the up comming VS2005?
Thanks,
Brien King
Nov 21 '05
20 5074
DWS
Brien,
Your right the sub or function can modify the objects you pass to em!

Byval the sub can modify a temporary copy of the object.
Byref the sub can modify the object.

Good Luck
DWS



Byval = a copy of the object. its just a copy so when you return from the
sub or function the original object is not changed.

ByRef = the actual object.


Byref = the function can actually change the object.

"Brien King" wrote:
If I have a parameter that has an Object type (as opposed to something like
a string), can I make that parameter a CONST?

Right now, if you pass an object into a sub/function, that sub/function can
modify the object no matter how it's defined (ByVal or ByRef).

In some cases, I want to make sure that you cannot modify the object. Is
there a way to do that in VS2003 or the up comming VS2005?
Thanks,
Brien King

Nov 21 '05 #11
On 2005-09-17, m.posseth <mi*****@nohaus ystems.nl> wrote:

No i am not missing the point ,
OK, then I misunderstood this...
why the $##$$# would you do that if you could just as easily use a local (
in the same class as the method ) contstant value


A local (or remote) constant value has no relevance to const parameters.
i do understand that what he want is not possible in this way , however i
provided a workaround to acomplish something simular
however after rethinking my first solution i thought that there is a much
better way

If i rethink this in a OOP way it isn`t so difficult to acomplish at all
( define the needed business rules in some objects on demand ) ,,, however
it is still a workaround but it will do what he wants


I'm curious what you mean here, but I must admit I'm not sure what
you're getting at or what it's relevance is.
Nov 21 '05 #12
Hmm well we are obviously not talking the same language :-)

when i said
why the $##$$# would you do that if you could just as easily use a local
(
in the same class as the method ) contstant value

I was refering to the previous answer i had given ( as i feel that
encapsulation in a class is a much better aproach )
I'm curious what you mean here, but I must admit I'm not sure what
you're getting at or what it's relevance is.
Please tell me what is wrong with defining a class with friend / public
readonly properties that can only be set during construction from the
outside , as a workaround for the problem
Your answer was No and i have given an alternative aproach ,,,, and after
rereading the original question i believe it is verry relevant .... if you
feel that it isn`t then let the TS decide if this might be an alternative ,
as it is obvious that he comes here for a solution of his problem .

regards

Michel Posseth



"david" <da***@woofix.l ocal.dom> wrote in message
news:sl******** **********@loca lhost.localdoma in... On 2005-09-17, m.posseth <mi*****@nohaus ystems.nl> wrote:

No i am not missing the point ,


OK, then I misunderstood this...
why the $##$$# would you do that if you could just as easily use a local
(
in the same class as the method ) contstant value


A local (or remote) constant value has no relevance to const parameters.
i do understand that what he want is not possible in this way , however i
provided a workaround to acomplish something simular
however after rethinking my first solution i thought that there is a much
better way

If i rethink this in a OOP way it isn`t so difficult to acomplish at all
( define the needed business rules in some objects on demand ) ,,,
however
it is still a workaround but it will do what he wants


I'm curious what you mean here, but I must admit I'm not sure what
you're getting at or what it's relevance is.

Nov 21 '05 #13
On 2005-09-18, m.posseth <mi*****@nohaus ystems.nl> wrote:
Hmm well we are obviously not talking the same language :-)

when i said
why the $##$$# would you do that if you could just as easily use a local
(
in the same class as the method ) contstant value

I was refering to the previous answer i had given ( as i feel that
encapsulation in a class is a much better aproach )
I'm curious what you mean here, but I must admit I'm not sure what
you're getting at or what it's relevance is.


Please tell me what is wrong with defining a class with friend / public
readonly properties that can only be set during construction from the
outside , as a workaround for the problem


You're right, we're not talking the same language. The paragraph I said
I didn't understand what you meant was "If i rethink this in a OOP way
it isn`t so difficult to acomplish at all ( define the needed business
rules in some objects on demand )", which is a whole lot more obscure
than what you just said. But no matter...
Your answer was No and i have given an alternative aproach ,,,, and after
rereading the original question i believe it is verry relevant .... if you
feel that it isn`t then let the TS decide if this might be an alternative ,
as it is obvious that he comes here for a solution of his problem .


No, Jesse posted more or less the same thing. Wrapping the object in
read-only class or interface is the standard response given to this,
and it's certainly relevant.

Of course, it's also quite limited and not really a reasonable replacement
at all except in very simple circumstances. All in all, I agree with
the arguments against const parameters that people like Anders Hejlberg
have made, but it's a very useful construct that isn't easily duplicated
in .Net, and I think the idea that the equivalent is somehow easy to
accomplish is simply wrong.

Nov 21 '05 #14
DWS,

Sorry, but that is not what happens, and thus my query. In .NET, if you
pass an object ByVal, it does NOT copy the object itself and the original
object DOES get modified. I have tested this, and it's easy to demonstrate.

Here is a quick example:

Public Class TTestClass
Private m_myVar As String
Property MyVar() As String
Get
Return m_myVar
End Get
Set(ByVal Value As String)
m_myVar = Value
End Set
End Property
End Class

Private Sub Button4_Click(B yVal sender As System.Object, ByVal e As
System.EventArg s) Handles Button4.Click
Dim testClass As New TTestClass
Dim testString As String

ModifyMyTestCla ss(testClass)
MessageBox.Show (testClass.MyVa r)

ModifyMyTestStr ingByVal(testSt ring)
MessageBox.Show (testString)

ModifyMyTestStr ingByRef(testSt ring)
MessageBox.Show (testString)

End Sub

Private Sub ModifyMyTestCla ss(ByVal P_testClass As TTestClass)
P_testClass.MyV ar = "This shouldn't leave this method... but it does!"
End Sub

Private Sub ModifyMyTestStr ingByVal(ByVal P_testString As String)
P_testString = "This won't leave this method..."
End Sub

Private Sub ModifyMyTestStr ingByRef(ByRef P_testString As String)
P_testString = "This WILL leave this method..."
End Sub

When you run that code, you'll see the message "This shouldn't leave this
method... but it does!".

Technically, a String is an Object in .NET so you would expect the same
behaviour, but it behaves as I would expect it to. The first Message box is
Empty, the second has a value.

Now I had made the wrong assumption up to this point, that it actually made
a copy of the object that is being passed in and that it wouldn't modify the
original object. If I wanted to modify the original object I would have had
to specify ByRef. To me is was the same as passing in someting in C++ as &
or passing in as const &. Apparently I was wrong.

I don't need to work around it, I just needed to be aware that is the way it
works. Because it behaves this way, it would be EASY to introduce subtle
bugs into the code due to the fact that it DOES modify the original object.

It would be Nice to be able to specify that a Parameter is CONST so that the
compiler would catch an assignment to that parameter. The main reason for
me to use that is to ensure that every INPUT parameter is exactly that.
INPUT.

Brien King
"DWS" <DW*@discussion s.microsoft.com > wrote in message
news:2F******** *************** ***********@mic rosoft.com...
Brien,
Your right the sub or function can modify the objects you pass to em!

Byval the sub can modify a temporary copy of the object.
Byref the sub can modify the object.

Good Luck
DWS

Byval = a copy of the object. its just a copy so when you return from the
sub or function the original object is not changed.

ByRef = the actual object.


Byref = the function can actually change the object.


Nov 21 '05 #15
David,

Yes, this is exactly what I was getting at. To me it's a safety issue more
then anything. I want to avoid introducing subtle bugs that are hard to
find. If you pass an object to someone elses method (say a 3rd Party), you
don't know what they are doing with your object and therefore you can't
guarntee the state of the object you passed in (unless you work around this
issue) once the method returns.
Brien King

"david" <da***@woofix.l ocal.dom> wrote in message
news:sl******** **********@loca lhost.localdoma in...
On 2005-09-17, m.posseth <mi*****@nohaus ystems.nl> wrote:
after rethinking ,,, i provided a workaround but it is absolutely
useless

why the $##$$# would you do that if you could just as easily use a local
(
in the same class as the method ) contstant value

maybe we should ask you what is your situation in wich you think this is
needed ???

as i said i would not pass a contstant value at all to a parameter , i
would
make it a class construct parameter and call it with a read only property
in
the method itself


You're missing the point. What the OP wants is to pass reference types
to a method and ensure that the method won't change the contents of the
passed object. It would look something like this...

Public Sub Foo(ByConst list As ArrayList)

Console.Writeli ne(list(0)) ' This is OK
list(0) = "Hello" ' This would be a compile error
Because the list parameter is declared const, the Foo method can't
change the contents. This is a very common construct in C++, but
isn't supported in the CLR or the Framework.



regards

Michel Posseth [MCP]


"Brien King" <sp********@arc aderestoration. com> wrote in message
news:er******** ******@TK2MSFTN GP10.phx.gbl...
If I have a parameter that has an Object type (as opposed to something
like a string), can I make that parameter a CONST?

Right now, if you pass an object into a sub/function, that sub/function
can modify the object no matter how it's defined (ByVal or ByRef).

In some cases, I want to make sure that you cannot modify the object.
Is
there a way to do that in VS2003 or the up comming VS2005?
Thanks,
Brien King


Nov 21 '05 #16
On 2005-09-18, Brien King <sp********@arc aderestoration. com> wrote:

Technically, a String is an Object in .NET so you would expect the same
behaviour, but it behaves as I would expect it to. The first Message box is
Empty, the second has a value.
You're interpreting this wrong. A string is an object, and is passed
just like any other object in the system.

The only difference is that strings don't have any methods on them that
can actually modify the object. That's why they're immutable; it's not
any kind of special language construct, it's just the way the class is
defined. If you define your own class that has only ReadOnly properties
and methods, you can get this same behavior (in fact, that's exactly
what others have been mentioning as a partial workaround).
Now I had made the wrong assumption up to this point, that it actually made
a copy of the object that is being passed in and that it wouldn't modify the
original object. If I wanted to modify the original object I would have had
to specify ByRef. To me is was the same as passing in someting in C++ as &
or passing in as const &. Apparently I was wrong.
Yep, it's sort of like passing a pointer in C++. You can't modify the
pointer itself as it exists in the caller, but you can modify the object
the pointer points to.

Just to complicate things, ValueTypes like Structures in .Net *are*
copied when you declare a ByVal parameter. This can all seem a little
different at first glance, but parameter passing is a very important
concept to get your head around.

I don't need to work around it, I just needed to be aware that is the way it
works. Because it behaves this way, it would be EASY to introduce subtle
bugs into the code due to the fact that it DOES modify the original object.

It would be Nice to be able to specify that a Parameter is CONST so that the
compiler would catch an assignment to that parameter. The main reason for
me to use that is to ensure that every INPUT parameter is exactly that.
INPUT.

Brien King
"DWS" <DW*@discussion s.microsoft.com > wrote in message
news:2F******** *************** ***********@mic rosoft.com...
Brien,
Your right the sub or function can modify the objects you pass to em!

Byval the sub can modify a temporary copy of the object.
Byref the sub can modify the object.

Good Luck
DWS

Byval = a copy of the object. its just a copy so when you return from the
sub or function the original object is not changed.

ByRef = the actual object.


Byref = the function can actually change the object.

Nov 21 '05 #17
So, when I do

Dim myString as String
myString = "This is a string"

Does it create a new instance of a String type and assign that to myString?
or does it actually modify the object that is myString? If the first part
is true, that would explain the difference in behavior that I am seeing.

So, if the = does create a new instance of the type (or assigns the pointer
to another instance) then I could look at ByVal and ByRef as this; ByVal is
a Pointer to an Instance of a Class. ByRef is a Pointer to a Poitner to an
Instance of a Class. Correct?
Brien King
"david" <da***@woofix.l ocal.dom> wrote in message
news:sl******** **********@loca lhost.localdoma in...
On 2005-09-18, Brien King <sp********@arc aderestoration. com> wrote:

Technically, a String is an Object in .NET so you would expect the same
behaviour, but it behaves as I would expect it to. The first Message box
is
Empty, the second has a value.


You're interpreting this wrong. A string is an object, and is passed
just like any other object in the system.

The only difference is that strings don't have any methods on them that
can actually modify the object. That's why they're immutable; it's not
any kind of special language construct, it's just the way the class is
defined. If you define your own class that has only ReadOnly properties
and methods, you can get this same behavior (in fact, that's exactly
what others have been mentioning as a partial workaround).
Now I had made the wrong assumption up to this point, that it actually
made
a copy of the object that is being passed in and that it wouldn't modify
the
original object. If I wanted to modify the original object I would have
had
to specify ByRef. To me is was the same as passing in someting in C++ as
&
or passing in as const &. Apparently I was wrong.


Yep, it's sort of like passing a pointer in C++. You can't modify the
pointer itself as it exists in the caller, but you can modify the object
the pointer points to.

Just to complicate things, ValueTypes like Structures in .Net *are*
copied when you declare a ByVal parameter. This can all seem a little
different at first glance, but parameter passing is a very important
concept to get your head around.

I don't need to work around it, I just needed to be aware that is the way
it
works. Because it behaves this way, it would be EASY to introduce subtle
bugs into the code due to the fact that it DOES modify the original
object.

It would be Nice to be able to specify that a Parameter is CONST so that
the
compiler would catch an assignment to that parameter. The main reason
for
me to use that is to ensure that every INPUT parameter is exactly that.
INPUT.

Brien King
"DWS" <DW*@discussion s.microsoft.com > wrote in message
news:2F******** *************** ***********@mic rosoft.com...
Brien,
Your right the sub or function can modify the objects you pass to em!

Byval the sub can modify a temporary copy of the object.
Byref the sub can modify the object.

Good Luck
DWS

Byval = a copy of the object. its just a copy so when you return from
the
sub or function the original object is not changed.

ByRef = the actual object.


Byref = the function can actually change the object.

Nov 21 '05 #18
On 2005-09-18, Brien King <sp********@arc aderestoration. com> wrote:
So, when I do

Dim myString as String
myString = "This is a string"

Does it create a new instance of a String type and assign that to
myString? or does it actually modify the object that is myString? If
the first part is true, that would explain the difference in behavior
that I am seeing.
The first one. It creates a new String instance.

So, if the = does create a new instance of the type (or assigns the
pointer to another instance) then I could look at ByVal and ByRef as
this; ByVal is a Pointer to an Instance of a Class. ByRef is a
Pointer to a Poitner to an Instance of a Class. Correct?


Right. I think that's a very useful way to look at it.

Nov 21 '05 #19
well thanks for clearing things ,,,,,

I was already thinking that i might have understood something totally wrong
... ( as i am not a native english speaker ,,, this might happen )
But it is clear to me know where we started thinking different , I presented
the workaround as the easy solution to the question , while you who has
worked with the concept in C++ sees this as comparing apples with pears
:-) . obviously the two do not match at all, i do understand that
regards

Michel Posseth




"david" <da***@woofix.l ocal.dom> wrote in message
news:sl******** **********@loca lhost.localdoma in...
On 2005-09-18, m.posseth <mi*****@nohaus ystems.nl> wrote:
Hmm well we are obviously not talking the same language :-)

when i said
>why the $##$$# would you do that if you could just as easily use a
>local
>(
>in the same class as the method ) contstant value


I was refering to the previous answer i had given ( as i feel that
encapsulation in a class is a much better aproach )
I'm curious what you mean here, but I must admit I'm not sure what
you're getting at or what it's relevance is.


Please tell me what is wrong with defining a class with friend / public
readonly properties that can only be set during construction from the
outside , as a workaround for the problem


You're right, we're not talking the same language. The paragraph I said
I didn't understand what you meant was "If i rethink this in a OOP way
it isn`t so difficult to acomplish at all ( define the needed business
rules in some objects on demand )", which is a whole lot more obscure
than what you just said. But no matter...
Your answer was No and i have given an alternative aproach ,,,, and after
rereading the original question i believe it is verry relevant .... if
you
feel that it isn`t then let the TS decide if this might be an alternative
,
as it is obvious that he comes here for a solution of his problem .


No, Jesse posted more or less the same thing. Wrapping the object in
read-only class or interface is the standard response given to this,
and it's certainly relevant.

Of course, it's also quite limited and not really a reasonable replacement
at all except in very simple circumstances. All in all, I agree with
the arguments against const parameters that people like Anders Hejlberg
have made, but it's a very useful construct that isn't easily duplicated
in .Net, and I think the idea that the equivalent is somehow easy to
accomplish is simply wrong.

Nov 21 '05 #20

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

Similar topics

16
2499
by: Steven T. Hatton | last post by:
In the following code, the only way I can figure out to pass an array of const is by setting the template argument to const in the instanciation expression. It would be (or seem to me) better if I could set that qualifier in the function call. Can that be done? #include <iostream> using std::ostream; using std::cout;
2
1857
by: Ronald S. Cook | last post by:
In my service project I have this function: Public Function SelectPenTypeTest(Optional ByVal PenTypeID As String = Nothing) As String Implements IPen.SelectPenTypeTest If PenTypeID = String.Empty Then Return "You didn't pass anything" Else Return "You passed: " + PenTypeID End If End Function
0
9933
marktang
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...
0
9781
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,...
0
11121
Oralloy
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...
0
10734
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10836
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,...
0
10407
tracyyun
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...
0
5982
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4606
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
2
4210
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.