❔ can I make variable nullable withouse initializing it?
I have this method that connect to database and I am checking if it connected:
17 Replies
yes its possible. Other way is to dispose the object.
well if I do it like this I just get same warning
I think it make more sense to use using.
look at this code
https://github.com/behdad088/GraphQL-API-Example/blob/main/GraphQLAPI/SQL.Database/DatabaseFactory.cs#L74
use this when I call the method?
yes
You cannot create a nullable variable and return the same variable as non-nullable
assing null to it?
I mean, you can
But the compiler will warn you which is perfectly valid
I was told I need to eliminate all warnings
it is bad to keep them
Return a nullable MySqlConnection then
I also want to ask if I have 4 classes where I will use database then I just need to connect database in each class with method like that? Is there no way to somehow connect database to whole program
Nay. Optimally you'd inject the database connection into the classes that need it
but what if I make class purely for working with database and create that class object for each class
it would have connection to database as part of constructor
uh, i'm not sure if I can follow, but do you mean something like
Where
DatabaseClass
establishes the connection and where ConsumerClasses create the DatabaseClass?yes but I would make dbclass as field or variable for whole cosummer class
so I can call methods of it anywhere
and initilize it together with consumer class
I am not sure if it makes sense to do it
I am gonna use database only a little
Yes, you'd rather inject the DatabaseClass into the ConsumerClasses then
I am gonna use database only a littleDepending on what is "a little" you can also think about injecting the result of what you are querying into the consumer classes. But it depends
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.