-2

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#
    }
  • 1
    This is really basic steps in programming, learn to search in google ! – mybirthname Sep 29 '16 at 14:54
  • 1
    You either need an instance of whatever class it is defined in, or it needs to be a static method and you'd have to specify the class name before it. – juharr Sep 29 '16 at 14:56
  • Oh ok. I was looking for a way to keep clean code and categorize my code into something like a Module, but those aren't in C#. Which is why I had my class setup the way it was. – RockGuitarist1 Sep 29 '16 at 15:24
  • What you need is a book or website to learn how to code in C# – pix Sep 30 '16 at 06:50

1 Answers1

2

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.

vivek
  • 1,595
  • 2
  • 18
  • 35
  • So what you are saying is that it is probably best to put each on of these methods in their own Class and call them accordingly? I was used to using Modules in VB, which is why I thought this would work lol. – RockGuitarist1 Sep 29 '16 at 15:21
  • If you want to share same copy of a function, you can mark it as `static` and then you do not need to create objects for accessing the object. It will give you a feel of `module` in VB. – vivek Sep 29 '16 at 15:30
  • Ok, that makes it easier. Thank you! :) – RockGuitarist1 Sep 29 '16 at 15:31