.Net Core 6 - IMongoCollection, Suppression State Warning CS8602 Dereference of a possibly null reference.
-
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
-
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
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 topublic 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
-
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 topublic 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
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
-
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'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