0

I have a List View element listViewMedia in one class and want to update it with data from another file (Strings, basically) so, i have created a public method

 public void addToListViewMedia()
 {  
     listViewMedia.Items.Add(new ListViewItem("datafromotherfile"));
 }

in the first class to add data, but I cannot access it from the other class. What am I doing wrong? I already tried accessing the element directly by setting it to public itself, but that didn't work out either, not to mention the crappy style that would bring to my code. Updating it from the same class is working, btw...

Max
  • 12,622
  • 16
  • 73
  • 101
joko
  • 182
  • 2
  • 11
  • 2
    Sharing the rest of your code would probably help. Are your classes both public or accessible to one another? Are you getting an error in your IDE (assuming you are using one)? Please be more specific. – Jesse Dec 21 '13 at 10:06
  • 1
    please share the code how you are accessing `addToListViewMedia()` method – Sudhakar Tillapudi Dec 21 '13 at 10:08
  • I am trying to access it with a buttonclick event in VS 2013 and in Intellisense i can't find it and if I enter the method the error is does not exist in the current context, but it is public, so I should be able to access it, right? private void ausführenButton_Click(object sender, EventArgs e) { returnListViewMedia(); } – joko Dec 21 '13 at 10:19
  • 1
    Evidently that is not right if you are getting an error. And there is no mention of `returnListViewMedia()` in your original code example. – Jesse Dec 21 '13 at 10:20
  • the method name changed, i had it from another version, the name is now returnListViewMedia(); but it still doesn't work – joko Dec 21 '13 at 10:22
  • I don't know what your mistake is because you haven't given your readers much to go by. I think you will yield an answer faster if you share the code you are working with in a more complete context. – Jesse Dec 21 '13 at 10:25

1 Answers1

2

Problem : you can not access members of other class, even if they are declared as public.

Solution : you should access other class memebers with the help of instance variable.

Note: if you can provide more details/code we could help you in much better way.

Try This: Sample

Class A
{

   public void addToListViewMedia()
    {    
        listViewMedia.Items.Add(new ListViewItem("datafromotherfile"));
    }

}

Class B
{

     private void ausführenButton_Click(object sender, EventArgs e)
     { 
        A obj=new A();  //create instance variable.
        obj.addToListViewMedia();//access methods of A using instance variable
     } 

}
Sudhakar Tillapudi
  • 25,935
  • 5
  • 37
  • 67
  • OK, that worked. Thank you. So Public just means that you can access it in such a way and private methods cannot be accessed at all? – joko Dec 21 '13 at 10:38
  • 1
    `acceess modifier` means how the othermembers outside the `class` can access the memebrs. `pubic` is one of type that specifies any one outside the class can access it, private means no one from outside the class can access. in either of the case you should access the memebrers using the instance varinable – Sudhakar Tillapudi Dec 21 '13 at 10:40