constant parameter

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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
 
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
 
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.
 
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 said:
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.
 
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.
 
Eugene said:
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.
 
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 said:
Eugene said:
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.
 
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top