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. Web Development
  3. ASP.NET
  4. .Net Core 6 - IMongoCollection, Suppression State Warning CS8602 Dereference of a possibly null reference.

.Net Core 6 - IMongoCollection, Suppression State Warning CS8602 Dereference of a possibly null reference.

Scheduled Pinned Locked Moved ASP.NET
csharptutorialphpasp-netdatabase
4 Posts 2 Posters 34 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.
  • J Offline
    J Offline
    jkirkerx
    wrote on last edited by
    #1

    Been some years now since I worked with .Net Core 3, been busy with PHP 8, and was surprised at the new stuff in .Net Core 6, and it's insatiable appetite to point out null conditions, so I'm playing along with it for now. But I just can't wrap my head around this one, and I'm starring at it like a deer in the headlights. So I asked VS 2022 for suggestions, and it suggested converting my old code to these new statements. I read the documentation .. [Resolve nullable warnings | Microsoft Learn](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/compiler-messages/nullable-warnings?f1url=%3FappId%3Droslyn%26k%3Dk(CS8602)#possible-dereference-of-null) and I didn't see any anything that sort of matched what I'm doing here. I tried ... Right hand side ?? "_database maybe null" but it said that it can't be applied to operands of type IMongoCollection and strings. Perhaps someone else has run into this and can give me a pointer or a solid. But I'll keep researching this to see if I can find something. My old code example from .Net Core 3

    public IMongoCollection Accounts
    {
    get { return _database.GetCollection("Accounts"); }
    }

    My new code that VS 2022 Suggested

    internal class MongoDbContext : IDisposable
    {
    private IMongoDatabase? _database;

    public void MongoDBContext()
    {
            
        // Grab the MongoDB Connection and Database
        var rootObject = AppSettingsJson.GetSettings();
        var dbConnection = rootObject?.Settings?.DbConnection;
        var connString = "mongodb://" + dbConnection?.User + ":" + dbConnection?.Pass + "@" + dbConnection?.Host;
        var client = new MongoClient(connString);
        \_database = client.GetDatabase(dbConnection?.Database);
    
    }
    
    // MongoDB Database Models        
    // I don't understand how to perform a null check, or to satisfy the compiler for "\_database" - \_database may be null here CS8602
    public IMongoCollection Accounts => \_database.GetCollection("Accounts");
    public IMongoCollection Shippers => \_database.GetCollection("Shippers");
    public IMongoCollection Contacts => \_database.GetCollection("Contacts");
    public IMongoCollection Pallets => \_database.GetCollection("Pallets");
    public IMongoCollection Customers => \_database.GetCollection("Customers");
    public IMongoCollection BillOfLad
    
    J 1 Reply Last reply
    0
    • J jkirkerx

      Been some years now since I worked with .Net Core 3, been busy with PHP 8, and was surprised at the new stuff in .Net Core 6, and it's insatiable appetite to point out null conditions, so I'm playing along with it for now. But I just can't wrap my head around this one, and I'm starring at it like a deer in the headlights. So I asked VS 2022 for suggestions, and it suggested converting my old code to these new statements. I read the documentation .. [Resolve nullable warnings | Microsoft Learn](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/compiler-messages/nullable-warnings?f1url=%3FappId%3Droslyn%26k%3Dk(CS8602)#possible-dereference-of-null) and I didn't see any anything that sort of matched what I'm doing here. I tried ... Right hand side ?? "_database maybe null" but it said that it can't be applied to operands of type IMongoCollection and strings. Perhaps someone else has run into this and can give me a pointer or a solid. But I'll keep researching this to see if I can find something. My old code example from .Net Core 3

      public IMongoCollection Accounts
      {
      get { return _database.GetCollection("Accounts"); }
      }

      My new code that VS 2022 Suggested

      internal class MongoDbContext : IDisposable
      {
      private IMongoDatabase? _database;

      public void MongoDBContext()
      {
              
          // Grab the MongoDB Connection and Database
          var rootObject = AppSettingsJson.GetSettings();
          var dbConnection = rootObject?.Settings?.DbConnection;
          var connString = "mongodb://" + dbConnection?.User + ":" + dbConnection?.Pass + "@" + dbConnection?.Host;
          var client = new MongoClient(connString);
          \_database = client.GetDatabase(dbConnection?.Database);
      
      }
      
      // MongoDB Database Models        
      // I don't understand how to perform a null check, or to satisfy the compiler for "\_database" - \_database may be null here CS8602
      public IMongoCollection Accounts => \_database.GetCollection("Accounts");
      public IMongoCollection Shippers => \_database.GetCollection("Shippers");
      public IMongoCollection Contacts => \_database.GetCollection("Contacts");
      public IMongoCollection Pallets => \_database.GetCollection("Pallets");
      public IMongoCollection Customers => \_database.GetCollection("Customers");
      public IMongoCollection BillOfLad
      
      J Offline
      J Offline
      jkirkerx
      wrote on last edited by
      #2

      On the internet, I was looking at another persons code, and noticed he used _database as a property, then I combined that with the help on [Resolve nullable warnings | Microsoft Learn](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/compiler-messages/nullable-warnings?f1url=%3FappId%3Droslyn%26k%3Dk(CS8618)#nonnullable-reference-not-initialized), to satisfy cSharp, or the compiler or both. Not sure if it runs yet, haven't gotten that far. But changing the type offered documented solutions for this. It's gonna be a hard week learning this new stuff.

      namespace SimpleBol.Context.MongoDb
      {
      internal class MongoDbContext : IDisposable
      {
      private IMongoDatabase _database { get; set; } = null!; // I upgraded this
      private MongoClient _client { get; set; } = null!; // I upgraded this to

          public void MongoDBContext()
          {
              
              // Grab the MongoDB Connection and Database
              var rootObject = AppSettingsJson.GetSettings();
              var dbConnection = rootObject?.Settings?.DbConnection;
              var connString = "mongodb://" + dbConnection?.User + ":" + dbConnection?.Pass + "@" + dbConnection?.Host;
              
              \_client = new MongoClient(connString);
              \_database = \_client.GetDatabase(dbConnection?.Database);
      
          }
      
          // Simple Bol Database Models
          public IMongoCollection Accounts => \_database.GetCollection("Accounts");
      

      If it ain't broke don't fix it Discover my world at jkirkerx.com

      Graeme_GrantG 1 Reply Last reply
      0
      • J jkirkerx

        On the internet, I was looking at another persons code, and noticed he used _database as a property, then I combined that with the help on [Resolve nullable warnings | Microsoft Learn](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/compiler-messages/nullable-warnings?f1url=%3FappId%3Droslyn%26k%3Dk(CS8618)#nonnullable-reference-not-initialized), to satisfy cSharp, or the compiler or both. Not sure if it runs yet, haven't gotten that far. But changing the type offered documented solutions for this. It's gonna be a hard week learning this new stuff.

        namespace SimpleBol.Context.MongoDb
        {
        internal class MongoDbContext : IDisposable
        {
        private IMongoDatabase _database { get; set; } = null!; // I upgraded this
        private MongoClient _client { get; set; } = null!; // I upgraded this to

            public void MongoDBContext()
            {
                
                // Grab the MongoDB Connection and Database
                var rootObject = AppSettingsJson.GetSettings();
                var dbConnection = rootObject?.Settings?.DbConnection;
                var connString = "mongodb://" + dbConnection?.User + ":" + dbConnection?.Pass + "@" + dbConnection?.Host;
                
                \_client = new MongoClient(connString);
                \_database = \_client.GetDatabase(dbConnection?.Database);
        
            }
        
            // Simple Bol Database Models
            public IMongoCollection Accounts => \_database.GetCollection("Accounts");
        

        If it ain't broke don't fix it Discover my world at jkirkerx.com

        Graeme_GrantG Offline
        Graeme_GrantG Offline
        Graeme_Grant
        wrote on last edited by
        #3

        Why use private properties and not fields?

        Graeme


        "I fear not the man who has practiced ten thousand kicks one time, but I fear the man that has practiced one kick ten thousand times!" - Bruce Lee

        “I fear not the man who has practised 10,000 kicks once, but I fear the man who has practised one kick 10,000 times.” - Bruce Lee.

        J 1 Reply Last reply
        0
        • Graeme_GrantG Graeme_Grant

          Why use private properties and not fields?

          Graeme


          "I fear not the man who has practiced ten thousand kicks one time, but I fear the man that has practiced one kick ten thousand times!" - Bruce Lee

          J Offline
          J Offline
          jkirkerx
          wrote on last edited by
          #4

          I'm willing to learn, what would you have done with fields? I used the private property because that's what MongoDB sort of suggested in some new sample code to get started. In 2020, I was just getting started with .Net Core 3 and cSharp, when I picked up a PHP 8 project, and haven't written a line of cSharp in almost 3 years now, so I'm having to sort of start all over again with this project.

          If it ain't broke don't fix it Discover my world at jkirkerx.com

          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