VB.NET 中的扩展 With 关键字

发布于 2024-09-29 23:21:49 字数 703 浏览 7 评论 0原文

在我当前的项目中,每个人都根据变量和方法的含义和用途来命名它们。这对于快速理解代码很有好处,但是大量长变量名称会带来一些麻烦,例如将数据集复制到实体/对象时。所以,虽然你理解了代码,但可读性仍然受到打击。

veryLongVariableName.Id = datasetVeryLongVariableName.Id
veryLongVariablename.Something = datasetVeryLongVariableName.Something
etc.

使用 VB.NET 的 With 关键字会有所帮助。

With veryLongVariableName

    .Id = datasetVeryLongVariableName.Id
    .Something = datasetVeryLongVariableName.Something

End With

现在,我的问题是,有没有办法使用 With 或类似的东西,但同时用于多个变量?比如:

With veryLongVariableName As a, datasetVeryLongVariableName as b

    a.Id = b.Id
    a.Something = b.Something

End With

我完全支持描述性命名约定,但它们确实会使事情变得混乱。尤其是VB!

In my current project everyone names variables and methods after what they are and what they do. This is good for quickly understanding the code, but with a lot of long varible names comes some headaches, such as when copying a dataset to an entity/object. So, while you understand the code, the readability still takes a blow.

veryLongVariableName.Id = datasetVeryLongVariableName.Id
veryLongVariablename.Something = datasetVeryLongVariableName.Something
etc.

Using VB.NET's With keyword can help.

With veryLongVariableName

    .Id = datasetVeryLongVariableName.Id
    .Something = datasetVeryLongVariableName.Something

End With

Now, my question, is there any way of using With, or something similar, but for several variables at the same time? Something like:

With veryLongVariableName As a, datasetVeryLongVariableName as b

    a.Id = b.Id
    a.Something = b.Something

End With

I'm all for descriptive naming conventions, but they do tend to clutter things. Especially in VB!

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

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

发布评论

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

评论(3

荆棘i 2024-10-06 23:21:50

在幕后,With 关键字所做的只是声明一个引用您指定的对象的临时变量。它的引用将被编译为与完全限定对象名称完全相同的 IL 代码,因此不会造成性能损失。您可以自己轻松地完成此操作:

Dim a as MyClass = veryLongVariableName
Dim b as MyOtherClass = datasetVeryLongVariableName

a.Id = b.Id
a.Something = b.Something

Behind the scenes, all the With keyword does is declare a temporary variable that references the object you specify. Its references will be compiled to exactly the same IL code as fully-qualifying the object name, so there is no performance penalty. You can easily do this yourself:

Dim a as MyClass = veryLongVariableName
Dim b as MyOtherClass = datasetVeryLongVariableName

a.Id = b.Id
a.Something = b.Something
感性不性感 2024-10-06 23:21:49

如果执行这些分配集的代码是单个逻辑操作的一部分,那么您可以将其重构为它自己的方法/函数调用。虽然这并没有具体回答您关于倍数用法的问题,但它会根据流行书籍(例如 Code Complete)中的建议提高可读性。

If the code that does these sets of assignments is part of a single logical action then you could refactor it out into it's own method/function call. While this doesn't specifically answer your question about a multiple with usage, it would improve readibility in lines with suggestions found in popular books such as Code Complete.

瀞厅☆埖开 2024-10-06 23:21:49

不,这是不可能的。

With 仅适用于单个对象。请参阅 MSDN

但是,您可以嵌套 With 语句 (尽管不建议这样做,因为您最终可能会使您的代码变得非常不可读)。

No, this is not possible.

With works with a single object only. See MSDN.

You can however, nest With statements (though this is not recommended as you could end up making your code really unreadable).

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