Solved

How to call a method in a different class.

Posted on 2004-10-04
2
202 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 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

730 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