❔ can I make variable nullable withouse initializing it?

I have this method that connect to database and I am checking if it connected:
//Method connects to database
public MySqlConnection ConnectDatabase()
{
MySqlConnection? connection;
string config =
"server = 127.0.0.1;" +
"user = root;" +
"database = trivia_bot_data";
try
{
connection = new MySqlConnection(config);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
connection = null;
}
return connection;
}
//Method connects to database
public MySqlConnection ConnectDatabase()
{
MySqlConnection? connection;
string config =
"server = 127.0.0.1;" +
"user = root;" +
"database = trivia_bot_data";
try
{
connection = new MySqlConnection(config);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
connection = null;
}
return connection;
}
17 Replies
gondeman
gondeman15mo ago
yes its possible. Other way is to dispose the object.
Richard Evanson
Richard Evanson15mo ago
well if I do it like this I just get same warning
Richard Evanson
Richard Evanson15mo ago
use this when I call the method?
gondeman
gondeman15mo ago
yes
HimmDawg
HimmDawg15mo ago
You cannot create a nullable variable and return the same variable as non-nullable
Cattywampus
Cattywampus15mo ago
assing null to it?
HimmDawg
HimmDawg15mo ago
I mean, you can But the compiler will warn you which is perfectly valid
Richard Evanson
Richard Evanson15mo ago
I was told I need to eliminate all warnings it is bad to keep them
HimmDawg
HimmDawg15mo ago
Return a nullable MySqlConnection then
Richard Evanson
Richard Evanson15mo ago
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
HimmDawg
HimmDawg15mo ago
Nay. Optimally you'd inject the database connection into the classes that need it
Richard Evanson
Richard Evanson15mo ago
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
HimmDawg
HimmDawg15mo ago
uh, i'm not sure if I can follow, but do you mean something like
class DatabaseClass
{
...
}

class ConsumerClass1
{
public Foo GetFoo()
{
DatabaseClass dbClass = new();
}
}
class DatabaseClass
{
...
}

class ConsumerClass1
{
public Foo GetFoo()
{
DatabaseClass dbClass = new();
}
}
Where DatabaseClass establishes the connection and where ConsumerClasses create the DatabaseClass?
Richard Evanson
Richard Evanson15mo ago
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
HimmDawg
HimmDawg15mo ago
Yes, you'd rather inject the DatabaseClass into the ConsumerClasses then
I am gonna use database only a little
Depending 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 goblinnSip
Accord
Accord15mo ago
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.