473,569 Members | 3,015 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

constant parameter

Hi, how can I declare in the method parameter and pass an object using ByVal
and ensure that the called method cannot change the value of the object. I
wouldn't want the called method to change the data member of the class or
change the properties when windows form is passed in.

Eugene
Nov 21 '05 #1
8 2530
Eugene,

Who is making the called method?

If you don't want to do that , than just don't do it

Just my thought,
Cor
Nov 21 '05 #2
On 2005-09-06, Eugene <Eu****@discuss ions.microsoft. com> wrote:
Hi, how can I declare in the method parameter and pass an object using ByVal
and ensure that the called method cannot change the value of the object. I
wouldn't want the called method to change the data member of the class or
change the properties when windows form is passed in.


You can't. There are no const parameters in vb.net.
Nov 21 '05 #3
I meant MethodA calls MethodB with Obj1 as its parameter with ByVal. However,
MethodB is something like a plug-in and I want to ensure the other
programmers would not change Obj1 properties, but just can read and use the
data.

mmm... so David, is there no way to get such a restriction? any workaround?
Is this standard across all .net supported languages?

Anyone know if MS would add in the const parameter support for VB in the
future?

"Eugene" wrote:
Hi, how can I declare in the method parameter and pass an object using ByVal
and ensure that the called method cannot change the value of the object. I
wouldn't want the called method to change the data member of the class or
change the properties when windows form is passed in.

Eugene

Nov 21 '05 #4
"Eugene" <Eu****@discuss ions.microsoft. com> schrieb:
Hi, how can I declare in the method parameter and pass an object using
ByVal
and ensure that the called method cannot change the value of the object. I
wouldn't want the called method to change the data member of the class or
change the properties when windows form is passed in.


That's not supported, except for value type parameters where a copy of the
original object is generated if the value is passed by value.

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

Nov 21 '05 #5


One possibility for a workaround is to create an immutable verssion of
obj1's class and pass that through to MethodB

' Your obj1 class containing read/write properties
Public Class Foo

Public Property Bar() As String
Get
Return _bar
End Get
Set(ByVal Value As String)
_bar = Value
End Set
End Property

Private _bar As String

End Class
' An immutable version of Foo with read-only versions of the properties
Public Class FooImm

Public Sub New( _
ByVal foo As Foo _
)
_foo = foo
End Sub

Public ReadOnly Property Bar() As String
Get
Return _foo.Bar
End Get
End Property

Private _foo As Foo

End Class

NOTE: This can also be done by inheriting from Foo and Shadowing the
properties as readonly.
<in methodA>

...
MethodB(new FooImm(foo))
...

Private sub MethodB ( fooImm as FooImm )
<< has readonly access to all the properties in foo but cannot
update them >>
end sub
It is not a full replacement for const parameters and in many cases
cannot be used (e.g. where the object passed to MethodB must be of type
Foo) but can help in some situations.
hth,
Alan.

Nov 21 '05 #6
"Eugene" <Eu****@discuss ions.microsoft. com> wrote in message
news:04******** *************** ***********@mic rosoft.com...
I meant MethodA calls MethodB with Obj1 as its parameter with
ByVal. However, MethodB is something like a plug-in and I want to
ensure the other programmers would not change Obj1 properties,
but just can read and use the data.


Two choices.

Use Interfaces to define what the Developer is allowed to do
- and make all the properties on this [particular] Interface ReadOnly.
This is probably the safest approach.

Have your class provide a "data-only" version of itself, via a
property, something like

Public Class BigClass

' Here's an updatable property that we don't want tampered with
Public Property Thingy() as Integer
Get
Return m_iThingy
End Get
Set( Value as Integer )
m_iThingy = Value
End Set
End Property

Private m_iThingy as Integer = -1

' Property to get hold of the "safe" Data Class
Public ReadOnly Property ViewData() as DataClass
Return New DataClass( Me )
End Property

' The Data class that others should be reading
Public Class DataClass

Friend Sub New( ByVal oaParent as BigClass )
m_oParent = oaParent
End Sub

' ReadOnly version of the previously updatable property
Public ReadOnly Property Thingy() As Integer
Get
Return m_oParent.Thing y
End Get
End Property

Private m_oParent as BigClass = Nothing

End Class
End Class

then, Developers should only ever write functions that use the Data Class.

HTH,
Phill W.
Nov 21 '05 #7
Phill,

This construction bellow (if the method is made by others) can change the
value of an object and Eugene asks a solution for that
\\\
dim mydataset as new dataset
external.foreig nprocedure(myda taset)
///
The foreignprocedur e is
\\\
Private Sub foreignprocedur e(byval ds as dataset)
dim dt as table
ds.tables.add(d t) 'or whatever
End Sub
///

He has not made this class, he is using it and can not protect his object
even not by copying it if that is not serializable.

And because it is hidden and don't know what the foreignprocedur e does he
has a problem.

Cor

"Phill. W" <P.A.Ward@o-p-e-n-.-a-c-.-u-k> schreef in bericht
news:df******** **@yarrow.open. ac.uk...
"Eugene" <Eu****@discuss ions.microsoft. com> wrote in message
news:04******** *************** ***********@mic rosoft.com...
I meant MethodA calls MethodB with Obj1 as its parameter with
ByVal. However, MethodB is something like a plug-in and I want to
ensure the other programmers would not change Obj1 properties,
but just can read and use the data.


Two choices.

Use Interfaces to define what the Developer is allowed to do
- and make all the properties on this [particular] Interface ReadOnly.
This is probably the safest approach.

Have your class provide a "data-only" version of itself, via a
property, something like

Public Class BigClass

' Here's an updatable property that we don't want tampered with
Public Property Thingy() as Integer
Get
Return m_iThingy
End Get
Set( Value as Integer )
m_iThingy = Value
End Set
End Property

Private m_iThingy as Integer = -1

' Property to get hold of the "safe" Data Class
Public ReadOnly Property ViewData() as DataClass
Return New DataClass( Me )
End Property

' The Data class that others should be reading
Public Class DataClass

Friend Sub New( ByVal oaParent as BigClass )
m_oParent = oaParent
End Sub

' ReadOnly version of the previously updatable property
Public ReadOnly Property Thingy() As Integer
Get
Return m_oParent.Thing y
End Get
End Property

Private m_oParent as BigClass = Nothing

End Class
End Class

then, Developers should only ever write functions that use the Data Class.

HTH,
Phill W.

Nov 21 '05 #8
On 2005-09-06, Eugene <Eu****@discuss ions.microsoft. com> wrote:
I meant MethodA calls MethodB with Obj1 as its parameter with ByVal. However,
MethodB is something like a plug-in and I want to ensure the other
programmers would not change Obj1 properties, but just can read and use the
data.

mmm... so David, is there no way to get such a restriction? any workaround?
Others have mentioned the workarounds, wrap your data in a readonly
object or interface, or copy it before you hand it off.
Is this standard across all .net supported languages?
AFAIK, yes.
Anyone know if MS would add in the const parameter support for VB in the
future?
Pretty unlikely. Anders Hejlberg, the C#/CLR architect, seems pretty
opposed to it.

"Eugene" wrote:
Hi, how can I declare in the method parameter and pass an object using ByVal
and ensure that the called method cannot change the value of the object. I
wouldn't want the called method to change the data member of the class or
change the properties when windows form is passed in.

Eugene

Nov 21 '05 #9

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

Similar topics

2
6517
by: srinivas reddy | last post by:
Hi, Can a constant member function be overloaded with its non-constant equivalent. I mean can void func() be overloaded with void func() const. If so what behaviour is guaranteed, how does the compiler resolve ambiguity?, and how could I call a specific variant? tia, Srinivas
4
6175
by: Günter Zöchbauer | last post by:
can anyone tell why is valid but produces compiler error: An attribute argument must be a constant expression, typeof expression or array creation expression
8
2456
by: Rickard Andersson | last post by:
What it the use of constant references in methods and functions? Is it a design issue or hwat? A reference that cannot be edited? huh? Why pass a reference then?
12
2726
by: Kristian Bisgaard Lassen | last post by:
Hi, How do I parameterize a template by a a allocated array of integers, which is declared static and constant, so I can make compile time optimizations depending on the content of the array. The way I have written my code makes g++ complain about it not being static. BTW I am new to the C++ language and also the templates it provides. ...
8
3139
by: Tony Johansson | last post by:
Hello Experts! What does this mean actually. If you have a template with a type and non-type template argument, say, like this template<typename T, int a> class Array {. . .}; then A<int, 1> and A<int, 2> are different types. Now, if the A template
11
5266
by: lovecreatesbeauty | last post by:
Hello experts, Is const_cast only applied to pointers or references? If I have a constant object, then how can I remove constant attribute from it? #include <vector> #include <string> using namespace std;
2
6137
by: Johm | last post by:
Can i use a constant in a query ? I have a constant Public Const rate As Double = 10 Can i use it in the query ? I want to multiply the field with the constant rate, but Access does not accept the rate as a constant, instead it always put it into brackets as it is a field. I tried to write rate in sql, but after that access shoewed...
6
35659
by: fctk | last post by:
hello, i'm trying to compile this small program: int main(void) { unsigned long int max; max = 4000000000;
7
3158
by: icosahedron | last post by:
Is there a way to determine if a parameter to a function is a constant (e.g. 2.0f) versus a variable? Is there some way to determine if this is the case? (Say some metaprogramming tip or type trait?) I have a function with an if statement, that I would like to optimize away somehow. I was hoping the compiler would do it for me, but it...
2
4875
by: MikeB | last post by:
Hello All, how can I set my selectparameter to a constant value within my constants class or to a value in my web.config file?
0
7619
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...
0
7930
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. ...
0
8138
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...
0
7983
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...
0
6290
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...
1
5514
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...
0
5228
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...
0
3651
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
950
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...

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.