VB6中的加法运算是通过后期绑定执行的吗?

发布于 2024-12-06 01:55:46 字数 290 浏览 1 评论 0原文

我有一些函数对变体数据类型执行加法操作

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

落日海湾 2024-12-13 01:55:46

这与早期/晚期绑定无关。我认为您指的是 value1value2 的数据类型?然而,这个问题很令人困惑,因为它被标记为 VB6,但您使用 Return 这不是 VB6 中的有效关键字

在 VB6 的情况下,value1value2 将是变体,因此它们可以是数字或字符串(甚至对象)。该函数还将返回一个 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 and value2? The question is confusing however as it is tagged VB6 but you use Return which is not a valid keyword in VB6

In the case of VB6 both value1 and value2 will be variants so they could be numeric or strings (or even objects). The function will also return a Variant

  • If you call the function with two strings - the sum result will be the concatenated string: value1value2
  • If you pass in two numeric values then the sum will be the arithmetic sum of the values: 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

你是年少的欢喜 2024-12-13 01:55:46

在函数中进行后期绑定调用的唯一情况是 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 use recordset("ID") instead of recordset.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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文