制作参数CONST? [英] Make a Parameter CONST?
问题描述
如果我有一个具有Object类型的参数(与
a string相反),我可以将该参数设为CONST吗?
现在,如果你将一个对象传递给一个子/函数,那么该子/函数可以修改对象,无论它是如何定义的(ByVal或ByRef)。
在某些情况下,我想确保您无法修改对象。
有没有办法在VS2003或者上传VS2005?
谢谢,
Brien King
2005-09-17,Brien King< sp ******** @ arcaderestoration.com>写道:如果我有一个具有Object类型的参数(而不是类似
字符串),我可以将该参数设为CONST吗?
这有点奇怪的措辞,字符串是一个对象,但答案是
没有。
现在,如果将对象传递给子/函数,该子/函数可以修改对象,无论它是如何定义的(ByVal或ByRef)。
在某些情况下,我想确保您无法修改对象。
有没有办法在VS2003或上传VS2005?
Nope。这似乎是CLR和.Net
语言设计师关注的事情之一,并决定他们只是不想要。
只是传递一个类或结构作为参数并定义一个属性为
readonly
现在你可以读取参数但是你不能改变它
私有类constVal
ReadOnly属性示例()
获取
返回这是一个常数值
结束获取
结束财产
>
结束班
私人子测试(ByVal x as constVal)
MsgBox(x.example)
End Sub
Private Sub Button1_Click(ByVal sender As System.Object,ByVal e As
System.EventArgs)处理Button1点击
Dim y As new constVal
test(y)
End Sub
问候
Michel Posseth >
" Brien King" < SP ******** @ arcaderestoration.com>在消息中写道
新闻:呃************** @ TK2MSFTNGP10.phx.gbl ...如果我有参数有一个Object类型(而不是像字符串那样的东西),我可以将该参数设为CONST吗?
现在,如果将对象传递给子/函数,那么sub / function
可以修改对象,无论它是如何定义的(ByVal或ByRef)。
在某些情况下,我想确保你不能修改对象。
有没有办法在VS2003或VS2上升?
谢谢,
Brien King
Brien King < SP ******** @ arcaderestoration.com>在消息中写道
新闻:呃************** @ TK2MSFTNGP10.phx.gbl ...如果我有参数有一个对象类型...我可以使该参数
CONST吗?
在某些情况下,我想确保您不能修改该对象。有没有办法在VS2003或者上传VS2005中做到这一点?
没有办法这样做,但可能有可接受的解决方法。你可以使用例如
来创建一个shadow结构,它包含你想要的值的成员
按值传递,以及你的类中生成该结构的方法。你
然后会传递结构,通过值传递。
-
Jesse Liberty
作者O''Reilly的.NET书籍
http://www.LibertyAssociates.com
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
On 2005-09-17, Brien King <sp********@arcaderestoration.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.
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.example)
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim y As New constVal
test(y)
End Sub
regards
Michel Posseth
"Brien King" <sp********@arcaderestoration.com> wrote in message
news:er**************@TK2MSFTNGP10.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
"Brien King" <sp********@arcaderestoration.com> wrote in message
news:er**************@TK2MSFTNGP10.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
这篇关于制作参数CONST?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!