❔ 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
gondeman2y ago
yes its possible. Other way is to dispose the object.
Jack Badassson
Jack BadasssonOP2y ago
well if I do it like this I just get same warning
Jack Badassson
Jack BadasssonOP2y ago
use this when I call the method?
gondeman
gondeman2y ago
yes
HimmDawg
HimmDawg2y ago
You cannot create a nullable variable and return the same variable as non-nullable
Cattywampus
Cattywampus2y ago
assing null to it?
HimmDawg
HimmDawg2y ago
I mean, you can But the compiler will warn you which is perfectly valid
Jack Badassson
Jack BadasssonOP2y ago
I was told I need to eliminate all warnings it is bad to keep them
HimmDawg
HimmDawg2y ago
Return a nullable MySqlConnection then
Jack Badassson
Jack BadasssonOP2y 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
HimmDawg2y ago
Nay. Optimally you'd inject the database connection into the classes that need it
Jack Badassson
Jack BadasssonOP2y 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
HimmDawg2y 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?
Jack Badassson
Jack BadasssonOP2y 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
HimmDawg2y 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
Accord2y 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.
Want results from more Discord servers?
Add your server