vb.net 检查变量是否为 NULL
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20418106/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
checking if variable is NULL
提问by Codemunkeee
I am working on a project with PDFSharp. Sadly as a VB.Net developer, the examples they provided were written in C#. I'm having a problem of checking if a variable is null
.
我正在使用 PDFSharp 开发一个项目。遗憾的是,作为 VB.Net 开发人员,他们提供的示例是用 C# 编写的。我在检查变量是否为null
.
On C#, the code is declared like this
在 C# 上,代码是这样声明的
PdfDictionary resources = page.Elements.GetDictionary("/Resources");
if (resources != null)
'do stuff here
Im having a problem with the second line,
我的第二行有问题,
if (resources !=null)
如果(资源!=空)
So far, this is what I've done on VB, and I have also read this Blogfrom sLaks.
到目前为止,这就是我在 VB 上所做的,并且我还阅读了 sLaks 的这篇博客。
Dim resources As New PdfDictionary?()
Dim 资源作为新的 PdfDictionary?()
But it is giving me some errors.
但它给了我一些错误。
Simply setting resources to nothing
would yield its default value, could be an int, or whatsoever. I wanted it to be compared to a NULL.
This is the full code.
简单地将资源设置为nothing
将产生其默认值,可以是 int 或任何其他值。我希望将其与 NULL 进行比较。
这是完整的代码。
回答by Allan S. Hansen
If you've done:
如果你已经完成了:
Dim resources As New PdfDictionary?()
Dim resources As New PdfDictionary?()
Then resources will not be nothing as you've just instantiated it to something.
那么资源将不会是什么,因为您刚刚将其实例化为某物。
What you're likely after is
你可能追求的是
Dim resources As PdfDictionary = page.Elements.GetDictionary("/Resources")
IF resources IsNot Nothing THEN
'do stuff
回答by Brandon
Dim resources As PdfDictionary = page.Elements.GetDictionary("/Resources")
IF Not resources Is Nothing THEN
'do stuff
Works as well. It's builder's choice on this one.
也有效。这是建造者的选择。
回答by Panu Oksala
To avoid many nested ifs and foreach i would success to do somekind of returning if resources is null. Like this:
为了避免许多嵌套的 ifs 和 foreach,如果资源为空,我会成功地进行某种返回。像这样:
If resources Is Nothing Then
Exit Sub / Return / Throw New Exception("Resources cannot be loaded")...
End If
... rest of code ..
... 其余代码 ..