As a novice – intermediate C# programmer, my debugging issues surround the most basic elements of C# Program Design. My project is a web-based, computerized trading system. Working with (2) API’s, 1 for pricing and the other for Orders my problem seems to be in code design and issues associated with multiple sessions.
I try to use Delegates for all events.
My current questions regarding Windows Forms and Object
-
Is this the correct way (or a ‘proper way’) to instantiate class objects & event delegates using aspx pages? (my examples often serve to confuse more than help, I realize).
public partial class admin_Admin : System.Web.UI.Page { private static Downloader dl = null; private SendOrderDelegate sendError; protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { if (dl == null) { Main(); } } } protected static void Main() { dl = new Downloader(); sendOrder = new Steury.Trading.SendOrderDelegate(dl.Order); } } -
When and How to use the Main() Method? What is the definition for using Main() in a complex Web Application with multiple programs (Pricing, Orders, BackTesting, Optimization), all running simultaneously and using different aspx pages??
Do I use Main() above for each aspx page, and again for each main Class program… Orders.cs, OrderShort.cs, BacktTest.cs, BackTestShort.cs, Optimize.cs, OptimizeShort.cs, or do I use Main Only for the 2 aspx pages that login to the Pricing and Orders Servers and then maintain static session variables that connect to the proper classes?
As you can tell, I am struggling with these concepts and have not found a resource that goes into detail for very complex scenarios. Most if not all use very simple class examples.. I have found this to be helpful Core C# and .NET http://flylib.com/books/en/4.253.1.1/1/ .
- Is there any good advice on when to use the Static Keyword? Should I use it for the Top Level Pages only? I am not deploying a multiple user project at this point.
Any other suggestions would be useful, constructive criticism included.
You don’t. Remove it. The Main-functionality (the code you want to run the very first time your whole website is run, i.e. Application), or when a certain user accesses it the first time, i.e. Session) is moved to global.asax. By default, this file is not in your web application project, but you can add it.
No, it is not. Though the
IsPostBackcheck is good (it means whether or not the page has been posted by a user, i.e., whether or not he clicked a submit button on your page, which posts back to the same page). The thing wrong here is the static main function. Consider every page a class (it is a class, really) and that the system instantiates that class for you. The place for initiation isPage_Init. But many people keep it simple and put that inPage_Load. These special methods are automatically called by the ASP.NET system when the page is loaded.That’s not easy to answer. In general, in a web page, you hardly ever use the static keyword, simply because many people and user simultaneously access your pages. However, utility classes and methods can sometimes be static. It’s a total different environment than normal windows applications, which typically run in a protected environment.
There are many great books around on ASP.NET. Some go into very complex scenarios. But considering the current phase you’re in, I’d advice you to stick to simple scenarios to begin with.