473,836 Members | 1,421 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 5066
DWS
Thus your query

"Brien King" wrote:
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 #21

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

Similar topics

16
2494
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
1856
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
10840
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
10546
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
10588
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
10254
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
6978
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5647
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...
0
5823
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4013
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3112
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.