Solved

How to call a method in a different class.

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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

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…
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…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

759 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