Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. How to check object==null for objects without Constructor?

How to check object==null for objects without Constructor?

Scheduled Pinned Locked Moved C#
questionhelptutorial
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    artisticcheese
    wrote on last edited by
    #1

    Hello, I need to do following. If object was not initialized then assign variable to it like code below. Right now it throws an error becouse MyCert is not initialized when compared to null. How do I do what I need to do? X509ChainElement MyCert; foreach (X509ChainElement cert in chain.ChainElements) { if ( MyCert == null) { MyCert = (X509ChainElement) cert; continue; } else { .....; } }

    W 1 Reply Last reply
    0
    • A artisticcheese

      Hello, I need to do following. If object was not initialized then assign variable to it like code below. Right now it throws an error becouse MyCert is not initialized when compared to null. How do I do what I need to do? X509ChainElement MyCert; foreach (X509ChainElement cert in chain.ChainElements) { if ( MyCert == null) { MyCert = (X509ChainElement) cert; continue; } else { .....; } }

      W Offline
      W Offline
      whizzs
      wrote on last edited by
      #2

      artisticcheese wrote:

      Hello, I need to do following. If object was not initialized then assign variable to it like code below. Right now it throws an error becouse MyCert is not initialized when compared to null. How do I do what I need to do? X509ChainElement MyCert; foreach (X509ChainElement cert in chain.ChainElements) { if ( MyCert == null) { MyCert = (X509ChainElement) cert; continue; } else { .....; } }

      The quickest way is to initialize your object at the beginning. I am assuming you will be doing some processing between your declaration and your foreach loop, otherwise it makes no sense to check for a null because you know it is uninitialized. So it would look like this: X509ChainElement MyCert = null; // do some processing and other things..... foreach (X509ChainElement cert in chain.ChainElements) { if ( MyCert == null) //has not been initialized somewhere before the loop { MyCert = (X509ChainElement) cert; continue; } else { .....; } } But what I don't understand about your code is that only the first element will ever be assigned to MyCert. Of course I only get to see a small snippet and this may be the functionality you want.

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups