1
votes

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(); } }

1
Will need to see the class declarations. - willaien
Are you really in the same class? - SLaks
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
@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
@Annie123 Are your partial classes located within the same namespace? - Chase

1 Answers

0
votes

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.