dotnet0824
asked on
C# Winform application (Program.cs file)
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
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
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...
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...
ASKER
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);
}
}
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);
}
}
ASKER
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
I really dont understand how it could serve us as entry point which people say its equivalent to a Module in visual basic
ASKER
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
We cant create instance of a class .. Thats what I am trying to do but as its static it doesnot allow
I don't really get you, do you mind posting your program.cs here?
ASKER
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.EnableVisualSt yles();
Application.SetCompatibleT extRenderi ngDefault( false);
Application.Run(new frmLogin()) ;
}
}
}
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.EnableVisualSt
Application.SetCompatibleT
Application.Run(new frmLogin()) ;
}
}
}
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.CheckIfFileExis ts("your_s tring_goes _here");
That will do the check before the form appears.
I'm assuming that CheckIfFileExists is contained/referenced inside frmLogin:
frmLogin activeForm = new frmLogin();
activeform.CheckIfFileExis
That will do the check before the form appears.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
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.
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?
>> 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?
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.
[
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.
class Program
{
public static void FunctionX(int a, int b)
{
// ...
}
}
Then you can call FunctionX like:
Program.FunctionX(a, b);