Solved

C# Winform application (Program.cs file)

Posted on 2008-11-03
13
3,088 Views
Last Modified: 2013-12-17
Hi,
I am working on visualstudio.NET 2008 Enterprise edition C# windows application.
I want to write some functions like in General.bas(visual basic) before any form load
which are global to the entire application.
I came to know that Program.cs is the file which servers as entry point for C# windows application.
I tried to instantiate a class in there which is local to the client application, as I have to invoke
some calls from the object. As its static class it wouldn't allow it. How can i get about it
0
Comment
Question by:dotnet0824
[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
  • 4
  • 3
  • 2
  • +3
13 Comments
 
LVL 18

Expert Comment

by:philipjonathan
ID: 22865533
Do you mean you want to write static functions?

class Program
{
    public static void FunctionX(int a, int b)
    {
        // ...
    }
}

Then you can call FunctionX like:
Program.FunctionX(a, b);
0
 
LVL 21

Expert Comment

by:silemone
ID: 22865550
if the functions you want to run occur before form load, you could

a)  create the vb class that holds these methods...sort of like a wrapper class and use them through some methods you created...i.e.  pass values to these methods via your class...
b)  create an instance of it in the .cs program and run values before the Application.Run command...
0
 

Author Comment

by:dotnet0824
ID: 22865554
I created a class known as Module.cs in the Project ... I have the below function in Module.cs
Now I want to call this function in program.cs (ENTRY POINT OF APPLICATION)
 Module _Mod = new Module() ----- (I cant do this as program.cs is static class) .. Hope its clear
Lets say   public bool CheckIfFileExists(string FilePathName)
        {
            try
            {
              FileInfo _File = new FileInfo(FilePathName);
              if (_File.Exists)
                    return true;
                else
                    return false;
            }
            catch (Exception ex)
            {
                return false;
                throw (ex);
            }

        }
0
Technology Partners: 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!

 

Author Comment

by:dotnet0824
ID: 22865588
Moreover I cant declare even variables in program.cs  as its static class.
I really dont understand how it could serve us as entry point which people say its equivalent to a Module in visual basic
0
 

Author Comment

by:dotnet0824
ID: 22865592
b)  create an instance of it in the .cs program and run values before the Application.Run command...]
We cant create instance of a class .. Thats what I am trying to do but as its static it doesnot allow
0
 
LVL 18

Expert Comment

by:philipjonathan
ID: 22865720
I don't really get you, do you mind posting your program.cs here?
0
 

Author Comment

by:dotnet0824
ID: 22865834
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace  TEST
{

   static class Program
    {      
   
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {          
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new frmLogin()) ;
           
        }
    }
}
0
 
LVL 15

Expert Comment

by:angus_young_acdc
ID: 22865856
So you want to call the CheckIfFileExists method before the form appears?  Simply add the following before Application.Run.

I'm assuming that CheckIfFileExists is contained/referenced inside frmLogin:

frmLogin activeForm = new frmLogin();
activeform.CheckIfFileExists("your_string_goes_here");

That will do the check before the form appears.
0
 
LVL 15

Accepted Solution

by:
angus_young_acdc earned 125 total points
ID: 22865862
Might aswell put in the snippet!
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
 
namespace  TEST
{
 
   static class Program
    {      
    
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {           
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            frmLogin activeForm = new frmLogin();
            activeform.CheckIfFileExists("your_string_goes_here");
            Application.Run(new frmLogin()) ;
            
        }
    }
}

Open in new window

0
 
LVL 7

Assisted Solution

by:keustermans
keustermans earned 125 total points
ID: 22865914

First I would like to say that you should not be adding any code to the program.cs file as the only real purpose it has is to start the default form.
you can create a static class  to expose the methods that you want to make available throughout the app.
Lets assume that you will call the class Utils
then you can have:
public static class Utils
{
        public bool CheckIfFileExists(string fileName)
        {
               // your code here
        }
}

you can then call it as follows anywhere inyour project:  
              bool exists = Utils.CheckIfFileExists(fileName.txt);
0
 
LVL 18

Expert Comment

by:philipjonathan
ID: 22867137
Is your Module class a static class? If it is not, then you can still call:  Module _Mod = new Module();     from Program.Main.

But then if you want to make something equal to module in visual basic, then make module a static class, just like what keustermans showed you with the Utils class.
0
 
LVL 13

Expert Comment

by:SameerJagdale
ID: 22867228
I assume you want to replicate module concept in vb.
>> I want to write some functions like in General.bas(visual basic) before any form load
which are global to the entire application.

I hope you can create a sealed class and then initialize it in main (program.cs).
does it make sense?
0
 
LVL 21

Expert Comment

by:silemone
ID: 22867253
well as I stated in my early post:

[
if the functions you want to run occur before form load, you could

a)  create the vb class that holds these methods...sort of like a wrapper class and use them through some methods you created...i.e.  pass values to these methods via your class...
b)  create an instance of it in the .cs program and run values before the Application.Run command...
]

and yes, its just simply making Module class static if you don't want to create an instance of it in order to use it.
0

Featured Post

Tutorials alone can't teach real engineering

So we built better training tools.

-Hands-on Labs
-Instructor Mentoring
-Scenario-Based Tests
-Dedicated Cloud Servers

All at your fingertips. What are you waiting for?

Question has a verified solution.

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

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…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…

688 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