简体   繁体   English

什么是VB.NET相当于C#的'using'块

[英]What is the VB.NET equivalent to C#'s 'using' block

I am coding in VB.NET. 我在VB.NET编码。

Currently, I am enclosing object initialization and usage in a Try/Catch block and then tear it down (dispose, close, set to nothing) in the Finally block. 目前,我在Try / Catch块中包含对象初始化和使用,然后在Finally块中将其拆除(dispose,close,设置为nothing)。 The problem is, a C# 'using' block is so easy to use and would clean things up in my code. 问题是,C#'using'块很容易使用,并且会在我的代码中清理它们。

Is there a VB.NET equivalent? 是否有VB.NET等价物? Or is my Finally block tear-down solution the only way? 或者是我的最终阻止解决方案唯一的方法?

It's the same, it's just: 它是一样的,它只是:

Using conn As New SqlConnection
    ....
End Using

http://msdn.microsoft.com/en-us/library/htd05whh(VS.80).aspx http://msdn.microsoft.com/en-us/library/htd05whh(VS.80).aspx

Public Sub setbigbold(ByVal c As Control)
    Using nf As New System.Drawing.Font("Arial", 12.0F, _
        System.Drawing.FontStyle.Bold)

        c.Font = nf
        c.Text = "This is 12-point Arial bold"
    End Using
End Sub

Er, Using ... End Using 呃, Using ... End Using

See MSDN for more info 有关详细信息,请参阅MSDN

Here is another StackOverflow question that deals with the exact same issue. 是另一个处理完全相同问题的StackOverflow问题。 If I'm not mistaken it's used in a very similar, if not the exact same, way as in C# though. 如果我没有弄错的话,它的使用方式与C#非常相似(如果不是完全相同)。

Hope this helps! 希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM