VB.NET 为什么要这样声明这个子例程?

发布于 2024-10-04 14:48:23 字数 458 浏览 2 评论 0原文

VB.NET 2010,.NET 4

我有一个基本问题:我在网上找到了一个子例程,如此声明:

Public Sub MyFunction(Of T As Control)(ByVal Control As T, ByVal Action As Action(Of T)) ...

我想知道声明的 (Of T As Control) 部分潜艇的名字。我看到 T 稍后用于指定 Control 的类型和 Action(Of T) 中,但为什么这样做而不是仅仅这样做:

Public Sub MyFunction(ByVal Control As Control, ByVal Action As Action(Of Control)) ...

子名称后面的部分是什么意思?它的目的是什么?非常感谢,并对我的无知感到抱歉。

VB.NET 2010, .NET 4

I have a basic question: I have a subroutine that I found somewhere online declared thusly:

Public Sub MyFunction(Of T As Control)(ByVal Control As T, ByVal Action As Action(Of T)) ...

I'm wondering about the (Of T As Control) part of the declaration after the sub's name. I see that T is used later in specifying the type of Control and in Action(Of T), but why is it done this way instead of just doing:

Public Sub MyFunction(ByVal Control As Control, ByVal Action As Action(Of Control)) ...

What does that part after the sub's name mean? What is its purpose? Thanks a lot and sorry for my ignorance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

逆流 2024-10-11 14:48:23

这是 VB.NET 的 通用方法 声明语法:

泛型类型是单一编程
适应执行的元素
不同的功能具有相同的功能
数据类型。当你定义一个泛型时
类或过程,你不必
为每个定义一个单独的版本
您可能想要的数据类型
执行该功能。

打个比方,螺丝刀套装带有
可拆卸的头部。你检查螺丝
您需要转动并选择
该螺钉的正确头(开槽,
划线、加星号)。一旦您插入
螺丝刀头正确
处理,你执行完全相同的操作
与螺丝刀的功能,即
转动螺丝。

That is VB.NET's generic method declaration syntax:

A generic type is a single programming
element that adapts to perform the
same functionality for a variety of
data types. When you define a generic
class or procedure, you do not have to
define a separate version for each
data type for which you might want to
perform that functionality.

An analogy is a screwdriver set with
removable heads. You inspect the screw
you need to turn and select the
correct head for that screw (slotted,
crossed, starred). Once you insert the
correct head in the screwdriver
handle, you perform the exact same
function with the screwdriver, namely
turning the screw.

大姐,你呐 2024-10-11 14:48:23

(Of T) 是泛型类型参数,添加 As Control 会约束 T 的类型从 Control 继承。您可以用第二种方式编写该方法,但最终可能必须将 Control 强制转换为任何继承的类型(在 Action 中的 lambda 表达式中或在 MyFunction 主体中)。泛型可以让你避免这种情况。

例如:

Sub Main()
    Dim form As New Form()

    Dim textBox As New TextBox
    Dim listBox As New ListBox

    MyFunction(textBox, Sub(c) c.Text = "Hello")
    MyFunction(listBox, Sub(c) c.Items.Add("Hello"))

    MyFunction2(textBox, Sub(c) c.Text = "Hello")
    MyFunction2(listBox, Sub(c) CType(c, ListBox).Items.Add("Hello"))


End Sub

Public Sub MyFunction(Of T As Control)(ByVal Control As T, ByVal Action As Action(Of T))
    Action(Control)
End Sub

Public Sub MyFunction2(ByVal Control As Control, ByVal Action As Action(Of Control))
    Action(Control)
End Sub

在简单的情况下它看起来不太有价值,但对于更复杂的情况来说它的价值是无价的。

(Of T) is a generic type parameter, adding As Control constrains the type of T to inherit from Control. You could write the method the second way, but you'd probably end up having to cast the Control to whatever inherited type, within the lambda expression in the Action, or in the body of MyFunction. Generics allow you to avoid that.

For example:

Sub Main()
    Dim form As New Form()

    Dim textBox As New TextBox
    Dim listBox As New ListBox

    MyFunction(textBox, Sub(c) c.Text = "Hello")
    MyFunction(listBox, Sub(c) c.Items.Add("Hello"))

    MyFunction2(textBox, Sub(c) c.Text = "Hello")
    MyFunction2(listBox, Sub(c) CType(c, ListBox).Items.Add("Hello"))


End Sub

Public Sub MyFunction(Of T As Control)(ByVal Control As T, ByVal Action As Action(Of T))
    Action(Control)
End Sub

Public Sub MyFunction2(ByVal Control As Control, ByVal Action As Action(Of Control))
    Action(Control)
End Sub

It doesn't look too valuable in trivial cases, but it's invaluable for more complex cases.

一人独醉 2024-10-11 14:48:23

正如其他人所说,它是一个受约束的通用参数。但还没有人回答你问题的这一部分:

为什么要这样做

答案就在行动中。如果它只是声明为 Control,您将无法执行类似的操作,因为并非所有控件都有 .Text 属性*

MyFunction(MyTextBox, Function(t) t.Text = "new value" )

函数的主体只需要知道它是处理某种类型的控件,但传递给函数的 Action(Of T) 可能想知道控件的实际类型。

是的,所有控件都有 .Text 属性。让我们假装有些人没有这样做

As others have said, it's a constrained generic parameter. But no one has yet addressed this part of your question:

why is it done this way

The answer is in the action. If it were just declared as a Control, you wouldn't be able to do something like this, because not all controls have a .Text property*:

MyFunction(MyTextBox, Function(t) t.Text = "new value" )

The body of the function just needs to know that it's working on a control of some kind, but the Action(Of T) you pass to the function might want to know the actual type of the control.

Yes, all controls do have a .Text property. Let's pretend for a moment that some didn't

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