Alan Alan - 3 months ago 10
C# Question

How can I execute a method from inside my main method in C#

I have this code:

public static void Main(string[] args)
{
Console.WriteLine("Hello World!");
GetDatabaseConnection()
}

public SqliteConnection DatabaseConnection = null;

public SqliteConnection GetDatabaseConnection()
{
// code here
}


I would like to execute the GetDatabaseConnection method but I am getting an error saying: The type or namespace name cannot be found.

Answer

I think its likely to be due to you calling a instance method from your static main method. A static method cannot access non-static class level members. It has no "this" pointer. Try:

public static SqliteConnection DatabaseConnection = null;

public static SqliteConnection GetDatabaseConnection()
{
   // code here
}