ByRef Argument - Option Strict ON [英] ByRef Argument - Option Strict ON

查看:88
本文介绍了ByRef Argument - Option Strict ON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好:我在使用Option Strict ON的ByRef参数时遇到问题。我已经建立了一个通用的子程序ChangeValue()。如果新值与原始值不同,则更改

参数的值... To $ / $
适应所有变量类型我在ChangeValue中创建了参数( )类型

对象...我然后检查typecode并进行正确的比较等...


使用Option Strict ON我必须投通用对象的参数
$ $ $ $ $ $ $ $ $

问题是ByRef参数的值即使不是

虽然ChangeValue修改了参数??? CObj(lsValue)实际上

是否返回一个新的对象引用?


这里发生了什么???


任何见解都将不胜感激!!!


- 香农


''********** ***********

''*示例代码:

''*************** ******


Private Sub Button1_Click(ByVal sender As System.Object,ByVal e As

System.EventArgs)处理Button1.Click

Dim ls_Value As String =" 1"


Dim lo_Object As Object =" 1"


控制台。 WriteLine(ls_Value)


ChangeValue(CObj(ls_Value),2)


Console.WriteLine(ls_Value)


Console.WriteLine(" ***")

Console.WriteLine(lo_Object)


ChangeValue(lo_Object," 2")

Console.WriteLine(lo_Object)


End Sub


Private Sub ChangeValue(ByRef ao_Value As Object,ByVal ao_NewValue As

Object)


Dim le_TypeCode As TypeCode = Type.GetTypeCode(ao_Value.GetType)


选择案例le_TypeCode


Case TypeCode.String


If(CStr(ao_Value)<> CStr(ao_NewValue))然后


ao_Value = ao_NewValue


结束如果


结束选择


End Sub

Hello: I have a problem using ByRef arguments with Option Strict ON. I have
built a generic sub procedure "ChangeValue()" to change the value of an
argument if the new value is not the same as the original value...To
accommodate all variable types I made the arguments in ChangeValue() of type
object...I then check the typecode and do the correct comparison etc...

With Option Strict ON I have to cast the arguments to the generic object
type in the call to ChangeValue()

The problem is that the value of a ByRef argument does not change even
though ChangeValue modifies the argument??? Does CObj(lsValue) actually
return a new object reference?

What''s happening here???

Any insight will be greatly appreciated!!!

- Shannon

''*********************
''* Example Code:
''*********************

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim ls_Value As String = "1"

Dim lo_Object As Object = "1"

Console.WriteLine(ls_Value)

ChangeValue(CObj(ls_Value), "2")

Console.WriteLine(ls_Value)

Console.WriteLine("***")

Console.WriteLine(lo_Object)

ChangeValue(lo_Object, "2")

Console.WriteLine(lo_Object)

End Sub

Private Sub ChangeValue(ByRef ao_Value As Object, ByVal ao_NewValue As
Object)

Dim le_TypeCode As TypeCode = Type.GetTypeCode(ao_Value.GetType)

Select Case le_TypeCode

Case TypeCode.String

If (CStr(ao_Value) <> CStr(ao_NewValue)) Then

ao_Value = ao_NewValue

End If

End Select

End Sub

推荐答案



" Shannon Richards" < SR ********** @ hotmail.com>写了

"Shannon Richards" <sr**********@hotmail.com> wrote
您好:使用带有Option Strict ON的ByRef参数时遇到问题。我已经构建了一个通用的子程序ChangeValue()如果新值与原始值不同,则更改
参数的值...


这有点奇怪,为什么你需要一个例行程序要做到这一点?无论你在哪里打电话给那个例程,你都可以简单地完成任务....

ChangeValue(CObj(ls_Value)," 2")


ls_Value =" 2"


你知道要改变什么,为什么还要打扰常规?


为了适应所有变量类型,我在类型为
对象的ChangeValue()中创建了参数...然后我检查了类型代码并进行了正确的比较等...
Hello: I have a problem using ByRef arguments with Option Strict ON. I have
built a generic sub procedure "ChangeValue()" to change the value of an
argument if the new value is not the same as the original value...
That is a bit odd, why would you need a routine to do that? Wherever you
call that routine, you could simply do the assignment, instead....
ChangeValue(CObj(ls_Value), "2")
ls_Value = "2"

You know what to change it to, why bother calling out to a routine?

To accommodate all variable types I made the arguments in ChangeValue() of type
object...I then check the typecode and do the correct comparison etc...




如果无法进行内联,则会出现方法重载的情况:


Private Sub ChangeValue(ByVal Source As String,ByVal值为字符串)

源=价值

结束子

私有子变量值(ByVal源为整数,ByVal值为整数)

来源=价值

结束子

请注意我已删除了If<>测试因为它不需要,如果它们不同

则为Source分配新值。如果它们是相同的,则Source被赋予

相同的值,结果是相同的。同样地,不需要例行程序,你可以直接进行内联任务,而不是使用例程......


HTH

LFS



If it weren''t already possible to do inline, this would be a case for method overloading:

Private Sub ChangeValue(ByVal Source As String, ByVal Value As String)
Source = Value
End Sub
Private Sub ChangeValue(ByVal Source As Integer, ByVal Value As Integer)
Source = Value
End Sub
Take note that I''ve removed the If <> test because it is not needed, if they are different
then Source is assigned the new value. If they are the same, then Source is assigned
an identical value, the result is the same. Likewise, the routine is not needed, you could
just do the assignment inline, rather than using a routine....

HTH
LFS


感谢您的反馈...


此代码实际上是我的项目的一部分''使用单位

工作用于管理对象更改的模式等...这个程序做了很多

的额外处理,我没有表现出让事情变得简单...每个

时间属性在一个对象中更改此例程被调用,如果

新值实际上与原始值不同,许多额外步骤需要

place ...示例:将对象注册为脏在我的工作中

控制器......通过事件通知客户端更改等等


现在我已经恢复到一个重载的方法场景用于处理所有数据类型的
重载方法...字符串,整数,日期,


我只是好奇为什么我正在体验我正在经历的行为......使用

对象参数和ByRef与Option Strict ON ...

为什么这适用于VB.Net隐式数据类型转换(Option Strict OFF)

并且使用显式dataty表现不同pe cast(Option Strict ON)。

VB在幕后工作的内容是什么?明确地转换为

类型的对象?

谢谢

- Shannon

" Larry Serflaten" < SE ******* @ usinternet.com>在消息中写道

news:e%**************** @ TK2MSFTNGP09.phx.gbl ...
Thanks for your feedback...

This code is actually part of a project in which I''m using the "Unit of
Work" pattern for managing changes to objects etc...The procedure does a lot
of additional processing which I have not shown to keep things simple...Each
time a property in an object is changed this routine is called and if the
new value actually differs from the original many additional steps take
place...example: registering the object as dirty in my work
controller...notifying a client of changes via events...etc

Right now I have reverted to an overloaded method scenario where there are
overloaded methods for handling all datatypes...string, integer, date,
boolean etc etc etc...

I''m just curious why I''m experiencing the behavior I''m experiencing...using
Object arguments and ByRef with Option Strict ON...

Why does this work with VB.Net implicit datatype casting (Option Strict OFF)
and behave differently with explicit datatype casting (Option Strict ON).
What does VB do under the hood that''s different that explicitly casting to
type Object?

Thanks
- Shannon
"Larry Serflaten" <se*******@usinternet.com> wrote in message
news:e%****************@TK2MSFTNGP09.phx.gbl...

Shannon Richards < SR ********** @ hotmail.com>写了

"Shannon Richards" <sr**********@hotmail.com> wrote
您好:使用带有Option Strict ON的ByRef参数时遇到问题。我已经建立了一个通用的子程序ChangeValue()。如果新值与原始值不同,则更改
参数的值...
Hello: I have a problem using ByRef arguments with Option Strict ON. I
have
built a generic sub procedure "ChangeValue()" to change the value of an
argument if the new value is not the same as the original value...



这有点奇怪,为什么需要一个例程要做到这一点?无论你在哪里打电话给那个例程,你都可以简单地完成任务....



That is a bit odd, why would you need a routine to do that? Wherever you
call that routine, you could simply do the assignment, instead....

ChangeValue(CObj(ls_Value)," 2")
ChangeValue(CObj(ls_Value), "2")



ls_Value =" 2"

你知道要改变什么,为什么还要打扰常规?



ls_Value = "2"

You know what to change it to, why bother calling out to a routine?

为了容纳所有变量类型,我在ChangeValue()
对象类型中创建了参数...然后我检查了类型代码并进行了正确的比较等...
To accommodate all variable types I made the arguments in ChangeValue()
of type
object...I then check the typecode and do the correct comparison etc...

<如果它不可能内联,那么这将是
方法重载的一个例子:

Private Sub ChangeValue(ByVal Source As String,ByVal值为字符串)
Source = Value
End Sub
Private Sub ChangeValue(ByVal Source As Integer,ByVal Value As
Integer)
Source = Value
结束子

请注意我已删除了If<>测试因为它不需要,如果它们不同
那么Source被赋予新值。如果它们是相同的,那么Source
被赋予相同的值,结果是相同的。同样,例行程序不需要,你可以直接进行内联任务,而不是使用例行程序....

HTH
LFS



Shannon,

你需要为它支持的每种对象类型重载ChangeValue。
Shannon,
You need to overload ChangeValue for each object type that it supports.
Private Sub ChangeValue(ByRef ao_Value As String,ByVal ao_NewValue As
String)
...可能的公共逻辑...如果(ao_Value<> ao_NewValue)那么
ao_Value = ao_NewValue
结束如果
...可能的公共逻辑...结束Sub
Private Sub ChangeValue(ByRef ao_Value As Integer,ByVal ao_NewValue As
Integer)
...可能的共同逻辑......如果(ao_Value<> ao_NewValue)那么
ao_Value = ao_NewValue
结束如果
...可能的共同逻辑...结束Sub


在每个ChangeValue中,如果有共同逻辑(除了

比较),我会将每个特定的ChangeValue调用一般化

ChangeValue。


ByRef对象不适用于Option Strict On的原因是

例程要求你传递一个对象变量,你试图将b $ b传递给一个更具体的变量。如果你传递一个Integer变量并且

例程尝试为参数分配一个字符串,你得到一个类型不匹配

因为String不与Integer分配兼容,转换需要

,使用Option Strict此转换需要明确。 Ergo

您看到的错误...


希望这有帮助

Jay

" ; Shannon Richards < SR ********** @ hotmail.com>在消息中写道

news:uk ************** @ TK2MSFTNGP10.phx.gbl ...你好:我在使用Option Strict ON的ByRef参数时遇到问题。我已经建立了一个通用的子程序ChangeValue()如果新值与原始值不同,则更改参数的值...为了适应所有变量类型,我在
类型对象的ChangeValue()中创建了参数...然后我检查了类型代码并进行了正确的比较等等......

使用Option Strict ON我必须将参数转换为通用对象
类型调用ChangeValue()

问题是,即使ChangeValue修改了参数,ByRef参数的值也不会改变
CObj(lsValue)实际上是否会返回一个新的对象引用?

这里发生了什么?

任何见解都将受到高度赞赏!

- Shannon

''*********************
''*示例代码:
''*********************
私有子按钮1_Click(ByVal发送者为System.Object,ByVal e As
System.EventArgs)处理Button1.Click
Dim ls_Value As String =" 1"

Dim lo_Object As Object =" 1"

Console.WriteLine(ls_Value)

ChangeValue(CObj(ls_Value),2)

Console.WriteLine(ls_Value)

控制台。 WriteLine(***)

Console.WriteLine(lo_Object)

ChangeValue(lo_Object," 2")

控制台.WriteLine(lo_Object)

私有子ChangeValue(ByRef ao_Value As Object,ByVal ao_NewValue As
对象)

昏暗le_TypeCode As TypeCode = Type.GetTypeCode(a o_Value.GetType)

Select Case le_TypeCode

Case TypeCode.String

如果(CStr(ao_Value)<> CStr(ao_NewValue))然后

ao_Value = ao_NewValue

结束如果

结束选择

结束子
Private Sub ChangeValue(ByRef ao_Value As String, ByVal ao_NewValue As
String) ... possible common logic ... If (ao_Value <> ao_NewValue) Then
ao_Value = ao_NewValue
End If ... possible common logic ... End Sub Private Sub ChangeValue(ByRef ao_Value As Integer, ByVal ao_NewValue As
Integer) ... possible common logic ... If (ao_Value <> ao_NewValue) Then
ao_Value = ao_NewValue
End If ... possible common logic ... End Sub
Within each ChangeValue, if there is common logic (other then the
comparison) I would have each specific ChangeValue call a generalized
ChangeValue.

The reason that ByRef Object will not work with Option Strict On is that the
routine requires that you pass it an object variable, you are attempting to
pass it a more specific variable. If you pass it an Integer variable and the
routine attempts to assign a String to the parameter you get a type mismatch
as a String is not assignment compatible with an Integer, a conversion needs
to occur, with Option Strict On this conversion needs to be explicit. Ergo
the error you are seeing...

Hope this helps
Jay
"Shannon Richards" <sr**********@hotmail.com> wrote in message
news:uk**************@TK2MSFTNGP10.phx.gbl... Hello: I have a problem using ByRef arguments with Option Strict ON. I
have built a generic sub procedure "ChangeValue()" to change the value of
an argument if the new value is not the same as the original value...To
accommodate all variable types I made the arguments in ChangeValue() of
type object...I then check the typecode and do the correct comparison
etc...

With Option Strict ON I have to cast the arguments to the generic object
type in the call to ChangeValue()

The problem is that the value of a ByRef argument does not change even
though ChangeValue modifies the argument??? Does CObj(lsValue) actually
return a new object reference?

What''s happening here???

Any insight will be greatly appreciated!!!

- Shannon

''*********************
''* Example Code:
''*********************

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim ls_Value As String = "1"

Dim lo_Object As Object = "1"

Console.WriteLine(ls_Value)

ChangeValue(CObj(ls_Value), "2")

Console.WriteLine(ls_Value)

Console.WriteLine("***")

Console.WriteLine(lo_Object)

ChangeValue(lo_Object, "2")

Console.WriteLine(lo_Object)

End Sub

Private Sub ChangeValue(ByRef ao_Value As Object, ByVal ao_NewValue As
Object)

Dim le_TypeCode As TypeCode = Type.GetTypeCode(ao_Value.GetType)

Select Case le_TypeCode

Case TypeCode.String

If (CStr(ao_Value) <> CStr(ao_NewValue)) Then

ao_Value = ao_NewValue

End If

End Select

End Sub



这篇关于ByRef Argument - Option Strict ON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆