我怎么...我不太明白使用声明 [英] How do I...I do not quite understand the using statement

查看:55
本文介绍了我怎么...我不太明白使用声明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我似乎不太明白问号应该放在哪里。

我的代码如下:





MPOS =(60 - Len(COMPNAME)/ 2)



使用??? As ???

e.Graphics.DrawString(COMPNAME,New Font(Arial,12),New SolidBrush(Color.Black),MPOS,0)



结束使用



我的理由是使用上面因为我认为这是最好的处理方法。

是否有必要或者我应该忽略使用声明



我尝试过的事情:



不确定我应该尝试什么,因为我刚刚开始使用Graphics.Drawstring。所以这个

部分是新的

解决方案

基于MSDN文档:使用Statement(Visual Basic)| Microsoft Docs [ ^ ],您可以在代码需要非托管资源时使用该语句。但这并不意味着你必须使用该声明(没有义务)。 使用语句对程序员非常方便,因为行为类似于尝试...最后构造并注意处理对象...但是(!)有时你可能想要使用自定义错误处理程序,而不是使用语句。


你显示的代码除了Font和SolidBrush对象之外,不会创建任何需要Disposing的东西 - 所以你需要两个嵌套的Using块来处理它们:

 使用 f 作为字体= 字体(  Arial 12 
使用 MPOS,_ 作为画笔,b 作为画笔= SolidBrush(Color.Black)
e.Graphics.DrawString(COMPNAME,f,b,MPOS,< span class =code-digit> 0 )
结束 使用
结束 使用

每个使用块允许你声明一个变量,当它超出范围时会自动处理它的内容。

类似于使用Try ...最后一栏:

<前lang =vb> Dim f 作为 Font = 字体( Arial 12
尝试
Dim b As Brush = SolidBrush(Color.Black)
尝试
e.Graphics.DrawString(COMPNAME,f,b,MPOS, 0
最后
b.Dispose()
结束 尝试
最后
f。 Dispose()
结束 尝试

但是你不能意外地使用Disposed对象,因为它们超出范围且不可用。


I do not quite seem to understand what information should go where the question marks are.
My code is as follows:


MPOS =(60 - Len(COMPNAME) / 2)

Using ??? As ???
e.Graphics.DrawString(COMPNAME, New Font("Arial", 12), New SolidBrush(Color.Black), MPOS, 0)

End Using

My reasons are for using above as I believe it's the the best method of Disposing.
Is it necessary or should I just leave out the "Using Statement"

What I have tried:

Not sure what I should try, as I only just began using Graphics.Drawstring. So this
part is new

解决方案

Based on MSDN documentation: Using Statement (Visual Basic) | Microsoft Docs[^], you can use that statement when your code requires an unmanaged resource. But it does not mean that you have to use that statement (no obligatory). Using statement is very handy for programmers, because behaves like a Try...Finally construction and take care about disposing objects... But(!) sometimes you may want to use custom error handler, instead of Using statement.


The code you show doesn't create anything that would need Disposing except for the Font and SolidBrush objects - so you would need two nested Using blocks to handle them:

Using f As Font = New Font("Arial", 12)
    Using MPOS, _ As Brush, b As Brush = New SolidBrush(Color.Black)
        e.Graphics.DrawString(COMPNAME, f, b, MPOS, 0)
    End Using
End Using

Each Using block allows you to declare a variable, and will automatically Dispose of it's content when it goes out of scope.
It's similar to doing it with a Try...Finally block:

Dim f As Font = New Font("Arial", 12)
Try
    Dim b As Brush = New SolidBrush(Color.Black)
    Try
        e.Graphics.DrawString(COMPNAME, f, b, MPOS, 0)
    Finally
        b.Dispose()
    End Try
Finally
    f.Dispose()
End Try

But you can't accidently use the Disposed objects as they are out of scope and unavailable.


这篇关于我怎么...我不太明白使用声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆