?
Solved

C# Console application

Posted on 2012-09-13
5
Medium Priority
?
462 Views
Last Modified: 2012-09-25
Hi.

I am writing a console application that I would like to use to call a procedure from another program, instead of rewriting the code again.  I have added the program to my "References" file, but I am unsure of how to call the procedure from the other program.  Can anyone give me some samples on how to do this?  I am using Visual Studio 2008
0
Comment
Question by:Jasmin01
5 Comments
 
LVL 16

Expert Comment

by:Kalpesh Chhatrala
ID: 38394356
0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 1000 total points
ID: 38394413
So as you seem to be aware, a .NET executable can be referenced the same way a .NET DLL can be. The not-so-obvious trick to this (for those new to it) is that you don't enter the DLL the same way the system does when it wants to execute the application. Once you reference a .NET executable, you are effectively treating it as a library (DLL). How do you execute anything found within a library? You create instances of the classes or invoke the static methods found within the library.

All .NET executable have a class wrapping the Main method. This class is called "Program", by default. If there are any non-static methods found within Program, then you can create an instance of Program in order to execute any of those methods. If your intent is to call Main, then you simply preface the call to the static Main with the name of the class:  Program.Main(new string[] { "arg1", "arg2" });.

You would work with any other classes defined within the executable in a similar fashion.
0
 

Author Comment

by:Jasmin01
ID: 38394475
I have created the console application and I have referenced the exe application, like the code below:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AppMailer;

namespace Mailer
{
    class Program
    {
        static void Main(string[] args)
        {
        }
    }
}

I have to call the function found in my form1.vb file, but I don't know how to do this.  Can you help?
0
 
LVL 16

Assisted Solution

by:Kalpesh Chhatrala
Kalpesh Chhatrala earned 1000 total points
ID: 38394513
if form1.vb Class name is Form1 then you can call like below

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AppMailer;

namespace Mailer
{
    class Program
    {
        static void Main(string[] args)
        {
               Form1 oForm = new Form1();
               oForm.Show();
        }
    }
}
0
 
LVL 60

Expert Comment

by:Julian Hansen
ID: 38394525
@kaufmed - very nice explanation.
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

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!
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses
Course of the Month14 days, 5 hours left to enroll

809 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