473,657 Members | 2,597 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 #1
20 5039
On 2005-09-17, Brien King <sp********@arc aderestoration. com> 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?
That's kinda weirdly phrased, a string IS an Object, but the answer is
no.

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?


Nope. This seems to be one of those things that the CLR and .Net
language designers looked at and decided they just didn't want.

Nov 21 '05 #2
just pass a class or structure as the parameter and define a property as
readonly
now you can read the parameter but you can`t change it

Private Class constVal

ReadOnly Property example()

Get

Return "this is a constant value"

End Get

End Property

End Class

Private Sub test(ByVal x As constVal)

MsgBox(x.exampl e)

End Sub

Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As
System.EventArg s) Handles Button1.Click

Dim y As New constVal

test(y)

End Sub
regards

Michel Posseth

"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 #3
"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 ...can I make that parameter
a CONST?
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?

There is no way to do so, but there may be acceptable work-around. You can
for example create a shadow struct that has members for the values you want
to pass by value, and a method in your class to generate that struct. You
would then pass the struct, which is passed by value.
--
Jesse Liberty
Author of .NET books for O'Reilly
http://www.LibertyAssociates.com
Nov 21 '05 #4
"Brien King" <sp********@arc aderestoration. com> schrieb:
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?


No.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #5
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

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 #6
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 #7
David,

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.


And in my opinion very correct answered by you last week.

http://groups.google.com/group/micro...66eac3c?hl=en&

:-)

Cor
Nov 21 '05 #8
On 2005-09-17, Cor Ligthert [MVP] <no************ @planet.nl> wrote:
David,

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.


And in my opinion very correct answered by you last week.

http://groups.google.com/group/micro...66eac3c?hl=en&

:-)


Yes, the same questions do seem to pop up here quite often.

Nov 21 '05 #9

No i am not missing the point ,

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

well .... maybe i should have mentioned the impossibility in forehand , but
as i read the hole thread i base my answer on what is previously said
( sorry :-)

regards

and have a nice weekend

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:
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 #10

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

Similar topics

16
2463
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
1849
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
8305
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
8823
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
7321
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6163
isladogs
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...
0
5632
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
4151
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
4301
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1950
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1607
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.