VB6中的加法运算是通过后期绑定执行的吗?
我有一些函数对变体数据类型执行加法操作
Public Function Sum(value1, value2)
Sum = value1 + value2
End Function
此加法操作是否通过后期绑定执行?
或者仅当我在变体数据类型上调用某些方法但不使用二进制操作时才执行后期绑定?
我还想知道当我在 VB .NET 中添加对象数据类型(Option Strict 已关闭)、在 C# 中添加动态数据类型时是否使用了后期绑定。
谢谢。
I have some function which performs addition operation on to variant data types
Public Function Sum(value1, value2)
Sum = value1 + value2
End Function
Does this addition operation performed by late binding or not?
Or late binding performed only when I invoke some method on variant data type but not with binary operations?
I'm also wondering is late binding used when I add Object data types in VB .NET (Option Strict is turned off), dynamic data types in C#.
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这与早期/晚期绑定无关。我认为您指的是
value1
和value2
的数据类型?然而,这个问题很令人困惑,因为它被标记为 VB6,但您使用Return
这不是 VB6 中的有效关键字在 VB6 的情况下,
value1
和value2 将是变体,因此它们可以是数字或字符串(甚至对象)。该函数还将返回一个 Variant
value1value2
value1
+value2
始终最好避免这种类型的编程并定义您需要的类型以防止意外结果
编辑:
来自MS VB.NET 文档但这非常与 VB6 类似:
+ 运算符 (Visual Basic) 的主要目的是添加两个数字。但是,它也可以将数字操作数与字符串操作数连接起来。 + 运算符具有一组复杂的规则,用于确定是否添加、连接、发出编译器错误信号或引发运行时 InvalidCastException 异常。
这与早期或后期绑定无关,因为与编译器在编译时知道对象具有哪些方法、属性和事件(早期绑定)有关,而在编译时不知道这些(后期绑定)
后者可能会导致运行时错误,因为您可能输入了错误的方法名称,并且编译器无法识别它直到它尝试执行该行并且找不到您键入的方法。请查看以下内容以获取更多信息:早期绑定与后期绑定
This is not related to early /late binding. I think you are referring to the data types for
value1
andvalue2
? The question is confusing however as it is tagged VB6 but you useReturn
which is not a valid keyword in VB6In the case of VB6 both
value1
andvalue2
will be variants so they could be numeric or strings (or even objects). The function will also return a Variantvalue1value2
value1
+value2
Is is always best to avoid this type of programming and define the types that you require to prevent unexpected result
EDIT:
From MS VB.NET documentation but this is very similar for VB6:
The + Operator (Visual Basic) has the primary purpose of adding two numbers. However, it can also concatenate numeric operands with string operands. The + operator has a complex set of rules that determine whether to add, concatenate, signal a compiler error, or throw a run-time InvalidCastException exception.
This is nothing to do with early or late binding because that is to do with the compiler knowing which methods, properties and events an object has at compile time (early binding) and not knowing these at compile time (late binding)
The latter can result in runtime errors because you may have mistyped a method name and the compiler can't pick that up until it tries to execute the line and cannot find the method you typed. Have a look at the following for more information: Early vs Late Binding
在函数中进行后期绑定调用的唯一情况是 Variant 参数之一是对象引用。当对引用参数计算 + 运算符(或任何其他表达式)时,首先将其 DISPID_VALUE (0) 成员称为后期绑定(通过 IDispatch::Invoke),并在表达式中使用 retval 。如果 retval 是 IDispatch 引用,则递归调用 DISPID_VALUE。这就是为什么您可以在表达式(某种形式)中使用
recordset("ID")
而不是recordset.Fields.Item("ID").Value
。如果在表达式中使用已知类型(接口)的显式对象引用(例如 + 运算符),则编译器会发出早期绑定调用默认属性的代码,这会导致运行时的评估更加简单。
The only case when a late-bound call is made in your function is when one of the Variant arguments is an object reference. When evaluating the + operator (or any other expression) on a reference argument, first its DISPID_VALUE (0) member is called late-bound (through
IDispatch::Invoke
) and the retval is used in the expression. If the retval is an IDispatch reference it's DISPID_VALUE is called recursively. That's why you can userecordset("ID")
instead ofrecordset.Fields.Item("ID").Value
in expressions (kind of).If explicit object references of known types (interfaces) are used in an expression (e.g. + operator), the compiler emits code that calls default property early-bound which results in much simpler evaluation at run-time.