RockGuitarist1 RockGuitarist1 - 2 months ago 10
C# Question

Calling Method from Separate Class to MainWindow.cs

I guess doing this in VB is much easier, but how do I call a method that is another class? This is what I have inside of my Sql.cs:

public static MainWindow MainWindow;
public void FlLoadMembers()
{
SetConnection();
SqlCon.Open();
SqlCmd = SqlCon.CreateCommand();
const string commandText = "select firstname, lastname from members";
SqlAdapter = new SQLiteDataAdapter(commandText, SqlCon);
DsMembers.Reset();
SqlAdapter.Fill(DsMembers);
DtMembers = DsMembers.Tables[0];
MainWindow.lstPanelMembers.Items.Add(DtMembers);
SqlCon.Close();
}


Trying to call this in MainWindow.cs

private MainWindow()
{
InitializeComponent();
GetVersion();
FlLoadMembers(); //This doesn't work apparently in C#
}

Answer

FlLoadMembers() must be in a class. You need to create the object of that class and then you can call that function. So if your function FlLoadMembers() is in class named Class1, then you can do this:

Class1 obj = new Class1(); //Creating the object.
obj.FlLoadMembers();//Calling the function.

Hope it helps you.

Comments