Solved

How to call a method in a different class.

Posted on 2004-10-04
2
200 Views
Last Modified: 2010-04-15
This code generates the following error.   I'm new to c# and programming in general, so I'm not quite sure what needs to happen.

      public class confirm : System.Windows.Forms.Form    // this is the class that makes the call

private void button1_Click(object sender, System.EventArgs e)
            {
            AddOrder();     // this exists in       public class Form1 : System.Windows.Forms.Form


Here is the error I get, and both classes exist in POS.
            The name 'AddOrder' does not exist in the class or namespace 'POS.confirm

Thanks,
Sean

0
Comment
Question by:sean-keys
2 Comments
 
LVL 13

Accepted Solution

by:
BlackTigerX earned 50 total points
ID: 12223521
there are different ways of doing it, if they are in the same name space (like you opened a new project and added two forms, they are in the same namespace)
you can call any methods in the "form2" as long as they are public and static without having to do anything else

so the AddOrder() method would have to be defined as:

public static void AddOrder();

the PROBLEM there is, this AddOrder method would not be able to access anything in the public class Form1 : System.Windows.Forms.Form because is an STATIC method

if you wanted to be able to call AddOrder and be able to have access to everything in public class Form1 : System.Windows.Forms.Form
you then need to instantiate public class Form1 : System.Windows.Forms.Form

in code it would something like:

Form1 newform = new Form1(); //this is the class where AddOrder is (this is where you create an instance)
newform.AddOrder(); //then you are able to call methods of the other class

and your AddOrder can be defined as:

public void AddOrder()...
0
 

Author Comment

by:sean-keys
ID: 12223674
Excellent answer,  you made several things a lot clearer.

Thanks,
Sean
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now