1

I'm using Visual Studios. I wrote a method in a form1.cs file in a partial class

private void TestMethod1()
{
}

I want to call this method in form2.designer.cs, in the same partial class. I tried this:

TestMethod1(); 

but I got the error method not found.

this is the form.cs namespace classA { public partial class A : B {....
private void TestMethod1() { } } }

this is the form.designer.cs namespace classA { partial class A { private void InitializaCOmponent() { ..... } (where I call my function) TestMethod1(); } }

Annie123
  • 43
  • 8
  • 1
    Will need to see the class declarations. – willaien Jan 20 '16 at 22:53
  • 2
    Are you really in the same class? – SLaks Jan 20 '16 at 22:53
  • Turn it back to private, that's not the problem. Check if form1 and form2 really are the same, because just from the filenames it looks like those are two different classes. – pid Jan 20 '16 at 22:56
  • @pid I rolled back your edit as it was too big a change to the code in the question. – ChrisF Jan 20 '16 at 22:56
  • Yes it's true. I added one `{` and unintentionally changed the nature of the question. Thanks for fixing! – pid Jan 20 '16 at 22:58
  • 1
    @Annie123 Please add the partial class definition and the class/method where you are calling `TestMethod1`. What you are trying to do should work so there must be something wrong in the code you're not showing us. – ChrisF Jan 20 '16 at 22:58
  • 1
    @Annie123 Are your partial classes located within the same namespace? – Chase Jan 20 '16 at 23:04
  • Yes same namespaces. The only difference between the partial classes is that 1 uses inheritance and the other doesnt. – Annie123 Jan 20 '16 at 23:06
  • I did put in more info :) Thanks so much – Annie123 Jan 20 '16 at 23:21
  • You call a private method in a derived class in the same file? –  Sep 26 '19 at 18:15

1 Answers1

0

If the situation is as you described, then the compiler should not generate the error message as it is valid code.

However, if you try to use the visual editor, and you insert the call in your code inside the InitializeComponent method you will get an error.

This is caused by the Form editor not being able to call functions that are defined within the class you are actually editing - it is a bit restrictive about what you can do within that scope.

Jens Meinecke
  • 2,904
  • 17
  • 20
  • That helped a bit thanks. When I put my method outside of the InitializeComponent, I get a different error ("method must have a return type") even though my method has a void. private void test1() { } – Annie123 Jan 20 '16 at 23:14