checking if variable is NULL

26,350

Solution 1

If you've done:

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

Solution 2

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.

Solution 3

To avoid many nested ifs and foreach i would success to do somekind of returning if resources is null. Like this:

If resources Is Nothing Then
 Exit Sub / Return / Throw New Exception("Resources cannot be loaded")...
End If

... rest of code ..

Share:
26,350
Codemunkeee
Author by

Codemunkeee

I Stand Alone.

Updated on December 07, 2020

Comments

  • Codemunkeee
    Codemunkeee over 3 years

    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.

    On C#, the code is declared like this

    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 Blog from sLaks.

    Dim resources As New 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.