-1

I'm making c# app where I need to access method in partial class from another class.

To be more specific I want to add items to listview from class that is different from partial class but in same namespace.

I tried like this:

    public partial class Aa : Form
{
    public static void UpdateListView(string[] array)
    {
        if (ProcessNetUsageList.InvokeRequired)
        {
            ProcessNetUsageList.Invoke(new Action<string[]>(UpdateListView), array);
        }
        else
        {
            ListViewItem item = new ListViewItem(array[0]);
            for (int i = 1; i < 4; i++)
                item.SubItems.Add(array[i]);
            ProcessNetUsageList.Items.Add(item);
        }
    }
}

and then access it from another class like:

class Test
{
    test()
    {
        ProgramName.Aa.UpdateListView(someArray);
    }
}

But its giving an error because static method can only access static variables,and my listview isnt static(i created that listview in vs designer).

If i remove static keyword from method in partial class then i cant access it.I tried to create instance of partial class but without success.Any idea is welcome

note:Im using Invoke in my UpdateListView method because later that will be running on new thread

John Doe
  • 89
  • 1
  • 11
  • 4
    `"I tried to create instance of partial class but without success."` - Well, what did you try? Because to access an instance of an object you would indeed need an instance of that object. So where is the instance of your form? – David Aug 15 '17 at 16:34

2 Answers2

0

Remove the static keyword from UpdateListView, as you have done before. In test(), you need to instantiate Aa before you access UpdateListView.

Aa temp = new Aa()

You can then access the method by using

temp.UpdateListView(someArray);
0liveradam8
  • 752
  • 4
  • 18
  • I did try that too,but it gives me "Embedded statement cannon be a declaration or labeled statement" – John Doe Aug 15 '17 at 16:46
  • your answer is misleading, because simply creating a new object of type `Aa` will result in an empty object. This will not be the same as in the example. With it's own (probably empty) ListView – Mong Zhu Aug 15 '17 at 17:03
0

The nature of an object-oriented language is that objects don't have universal access to modify other objects. This is a good thing.

You've provided relatively little code so it's hard to provide a perfect answer here, but there are a few paradigms that resolve this issue.

One is to pass the instance to your test class, like this:

class Test
{
    test(ProgramName.Aa form)
    {
        form.UpdateListView(someArray);
    }
}

Or, if class test actually contains the ListView, you can pass that to a static method in Aa.

class Test
{
    ListView someListView;
    test()
    {
        ProgramName.Aa.UpdateListView(someListView, someArray);
    }
}

Ultimately, you should think about the logical relationship between these objects to determine how these objects should communicate.

Daniel
  • 1,695
  • 15
  • 33