?
Solved

How to call a method in a different class.

Posted on 2004-10-04
2
Medium Priority
?
205 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 13

Accepted Solution

by:
BlackTigerX earned 200 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Suggested Courses

764 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