Wrox Programmer Forums
Go Back   Wrox Programmer Forums > .NET > Other .NET > General .NET
|
General .NET For general discussion of MICROSOFT .NET topics that don't fall within any of the other .NET forum subcategories or .NET language forums.  If your question is specific to a language (C# or Visual Basic) or type of application (Windows Forms or ASP.Net) try an applicable forum category. ** PLEASE BE SPECIFIC WITH YOUR QUESTION ** When posting here, provide details regarding the Microsoft .NET language you are using and/or what type of application (Windows/Web Forms, etc) you are working in, if applicable to the question. This will help others answer the question without having to ask.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the General .NET section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old August 23rd, 2004, 10:06 AM
Registered User
 
Join Date: Aug 2004
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Static ctors in VB.NET, C# vs C++

.NET instantiates static declarations (class members, for example) differently than C++, which results in a very frustrating side effect. As a C++ programmer of 10 years (much of it using MFC), it was easy to properly initialize an abstract class factory with a derived class. This is because C++ instantiates all static declarations before executing main().

For example, the base class would contain 'protected static m_Instance' and a helper class would be used to create the proper derived instance for m_Instance, eg the application code would contain a static declaration of the helper class, and the helper classes ctor would contain something like 'BaseClass.SetInstance (new DerivedClass)'. This technique guaranteed that the factory would be initialized before main() was executed.

Unfortunately, .NET handles static fields differently, e.g. the ctor for the static class is not instantiated before main() executes. Instead, it is not until a member of the class is explicitly accessed do any static ctors execute, which prevents the above factory pattern from working correctly.

I've spent way too much time trying to find a 'hook' in .NET that I could use to execute some initialization code like the above BaseClass.SetInstance (new DerivedClass) snippet. I've looked at deriving from classes to override ctors/methods, hooking into events, etc. for classes like Assembly, Application, Processes, ProcessModule, AppDomain, etc. and still haven't been able to find a way to have a block of initialization code execute either 1) before the application starts running, or 2) after the assembly is loaded but before any references to it are resolved.

Any suggestions?





Similar Threads
Thread Thread Starter Forum Replies Last Post
ASP.NET STATIC PORT farsfar2 ASP.NET 2.0 Basics 2 November 25th, 2009 07:48 AM
non-static reports to static html files miamikk ASP.NET 2.0 Basics 0 June 4th, 2007 01:48 PM
ASP.NET - Static Page and Dynamic Page sohrabus ASP.NET 2.0 Professional 2 April 18th, 2007 11:42 PM
Callin Static Members from .NET Dll in VBA syoguran VB How-To 0 March 13th, 2006 04:37 AM
VB.Net question on Windows VB.Net datagrids dmsousa VS.NET 2002/2003 1 January 19th, 2005 02:45 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.