473,327 Members | 1,952 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

Running code at app startup - static variables

Hi,

I quite often need to have a system of classes providing alternative
implementions of some concept and need each class to register itself
with a manger (singleton) class. A good example might be some kind of
image loader, where you wrap the functionality to load an image of a
particular format in a class and a manager class maintains an
association between file extension and handler class.

In native C++ I would generally do this by having some kind of static
variable who's contructor would register a handler with the manager.

I tried doing the same in C++/CLI and came across the problem that
static member variables (and static constructors) don't get
initialized until the first time you use that class! Which of course
is a problem for me because I never intent on using that class
directly. The manager class would give me a pointer to a base class/
interface that all handle classes derive from.

Am I doing this all wrong? Is there a better way? Is there a way to
run code at app startup (without having to have a-priori knowledge of
each class)?
On a slightly different, but relevant, note, given that managed code
has (and exposes) a lot more knowledge about type information, is
there a way to store a reference to a type? I can see how that would
be useful for class factories, where you associate, say, an integer or
a string with a type. I had a quick look around the documention and I
could see how delegates (c'tors with standard interface) or even
System::Reflection::ConstructorInfo would be relevant, but long-
winded. Ideally something like :

Dictionary<int, Type>^ factory;

Type^ obj = factory[5]();
Thanks,

Bill


Jun 27 '08 #1
3 1360

Oooh....System::Activator::CreateInstance....how cool! :)

Still need a way of (automatically) registering types with the manager
class though.
Jun 27 '08 #2
Hmm, interesting, it appears that while I can't have a global variable
of a managed type, having a gcroot<T^is fine, i.e.

public ref class Foo
{...};

Foo^ g_Foo = gcnew Foo();

error C3145: 'g_Foo' : global or static variable may not have managed
type 'Foo ^'
may not declare a global or static variable, or a member of a
native type that refers to objects in the gc heap
however, the following works

gcroot<Foo^g_Foo = gcnew Foo();

and the Foo c'tor gets called during app startup.

Still, is there a better way of doing this? Doing the above doesn't
feel right for some reason. How would you do this sort of thing in C#
for example?

Jun 27 '08 #3
Still, is there a better way of doing this? Doing the above doesn't
feel right for some reason. How would you do this sort of thing in C#
for example?
You can tag the types with a custom attribute and then use reflection to
find and load them at startup.
Jul 21 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

45
by: Steven T. Hatton | last post by:
This is a purely *hypothetical* question. That means, it's /pretend/, CP. ;-) If you were forced at gunpoint to put all your code in classes, rather than in namespace scope (obviously classes...
2
by: Bryan Olson | last post by:
The current Python standard library provides two cryptographic hash functions: MD5 and SHA-1 . The authors of MD5 originally stated: It is conjectured that it is computationally infeasible to...
8
by: Paul Cochrane | last post by:
Hi all, I've got an application that I'm writing that autogenerates python code which I then execute with exec(). I know that this is not the best way to run things, and I'm not 100% sure as to...
6
by: N. Graves | last post by:
Thanks for taking the time to read my question and hopefully it will be understandable. I have a need to read a record in a table and place the information in public variables that can be used...
1
by: Peter Rilling | last post by:
Here is a weird behavior that I would not have expected in VS.NET during debugging. Maybe someone has some insight as to why this is happening. 1) I have a VS.NET web project. This contains a...
7
by: | last post by:
Hi, I have a structre of a database record, and I want to write a function that makes a statement for inserting it into a database. I have written make_statement_string() function for this, and...
4
by: tshad | last post by:
What would be a good way to check programmatically whether a service was running? We have a service that dies periodically and I need to check to see if this service is running. I know how to...
12
by: titan nyquist | last post by:
I have a class with data and methods that use it. Everything is contained perfectly THE PROBLEM: A separate thread has to call a method in the current instantiation of this class. There is...
2
by: Christof Warlich | last post by:
Hi, I'm working on a (template) library that is up to now entirely implemented in header-files. This makes the library quite convenient to use as no extra object code needs to be linked when...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.