[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

How to call a method in a different class.

Posted on 2004-10-04
2
Medium Priority
?
209 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

Enroll in October's Free Course of the Month

Do you work with and analyze data? Enroll in October's Course of the Month for 7+ hours of SQL training, allowing you to quickly and efficiently store or retrieve data. It's free for Premium Members, Team Accounts, and Qualified Experts!

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…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…

656 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