so many simple questions - how can I call the functions of my 1st cs file in 2nd cs file

  • Thread starter Thread starter Jessie
  • Start date Start date
J

Jessie

I defined some functions in one .cs file, i.e. ShowHelp(). Now I need use
them in my other cs files. How to do that?
This is a WinForm application.
Thanks.
 
Jessie,
Make sure your ShowHelp() method is declared as public, then just
instantiate the object in your other class and call the method. Since the
classes are in different .cs files, just make sure that you either put a
"using" namespace statement at the top of the other class, or fully qualify
the namespace of the class you want to instantiate.

example:

public class Class1
{
public void ShowHelp()
{
//code
}
}

public class Class2
{
private void CallOtherMethod()
{
Class1 myObject = new Class1();
myObject.ShowHelp();
}
}
 
Hi. I assume you have a class "Class1" with a ShowHelp() Method. To be able
to use it in another class "Class2" in the other cs file you need to declare
ShowHelp() as public:

---This is file class1.cs, for example
public class Class1
{
public void ShowHelp()
{
}
}

---This would be class2.cs, for example
public class Class2
{
Class1 cls1 = new Class1();
public void SomeMethod()
{
cls1.ShowHelp();
}
}

Hope this simple example helps.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top