473,382 Members | 1,705 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

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 2520
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****@discussions.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****@discussions.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****@discussions.microsoft.com> wrote in message
news:04**********************************@microsof t.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.Thingy
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.foreignprocedure(mydataset)
///
The foreignprocedure is
\\\
Private Sub foreignprocedure(byval ds as dataset)
dim dt as table
ds.tables.add(dt) '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 foreignprocedure 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****@discussions.microsoft.com> wrote in message
news:04**********************************@microsof t.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.Thingy
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****@discussions.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
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...
4
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
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
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....
8
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,...
11
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>...
2
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...
6
by: fctk | last post by:
hello, i'm trying to compile this small program: int main(void) { unsigned long int max; max = 4000000000;
7
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...
2
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?
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.