473,513 Members | 2,537 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

need to init data stored in static class (use constructor? what if it fails?)

I need to initialize data to be stored in a static class (for all my
code to see), but I'm lost on how to do so.

If I use the class constructor, what if it fails? I am reading it
information from files that (could potentially) not exist. If it
throws an exception, it could be thrown wherever the first access to
the class is, which is undetermined. This seems like a bad idea.

Furthurmore, the data initialization code should be run at the
beginning of the program, to ensure if anything fails, it fails right
away.

How else could I do it? Any ideas?

I could create a method that the static class (the one that holds the
data) that I call when *I* choose to call it, right away at the
beginning of the program. That method could set a "dataInitialized"
boolean to be true, which ever access to the data could check (via
properties).

What do you think?

Titan

Mar 29 '07 #1
2 1661
Titan,

That's pretty much the best bet. If you are not concerned about your
code being run before you call this method, I would make the method empty
(do nothing) and then put the code in the static constructor for the class.

However, in order to control when the lifetime of things are created,
you have to make explicit calls (at least in this case).

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

<ti***********@gmail.comwrote in message
news:11**********************@n59g2000hsh.googlegr oups.com...
>I need to initialize data to be stored in a static class (for all my
code to see), but I'm lost on how to do so.

If I use the class constructor, what if it fails? I am reading it
information from files that (could potentially) not exist. If it
throws an exception, it could be thrown wherever the first access to
the class is, which is undetermined. This seems like a bad idea.

Furthurmore, the data initialization code should be run at the
beginning of the program, to ensure if anything fails, it fails right
away.

How else could I do it? Any ideas?

I could create a method that the static class (the one that holds the
data) that I call when *I* choose to call it, right away at the
beginning of the program. That method could set a "dataInitialized"
boolean to be true, which ever access to the data could check (via
properties).

What do you think?

Titan

Mar 29 '07 #2
Thanks, it does help.

1) I do want to control when the data init happens, and...
2) I do NOT want to have the constructor failing (let's say because it
couldn't read a file it needs to init that data) at undetermined spots
in my program.

I guess I already have the solution. Just wanted to check with the
experts.

Titan

Mar 29 '07 #3

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

Similar topics

16
3132
by: Ed Sutton | last post by:
I use a mutex to disallow starting a second application instance. This did not work in a release build until I made it static member of my MainForm class. In a debug build, first instance got ownership, second did not and terminated. In a release build, the second instance *also* got ownership. I "fixed" it by making the mutex a static...
10
3857
by: Wylbur via DotNetMonster.com | last post by:
Hello to all of you geniuses, I'm having a problem trying to get an Init handler to fire for a Placeholder control at the initialization phase. I’ve posted this problem to 3 other ASP.NET forums, and noone wants to touch it. I tried to attach a literal control to a placeholder: <>-<>-<>-<>-<>-<>-<>-<>-<>-<>-<>-<>
5
3766
by: Learner | last post by:
Hello, Here is the code snippet I got strucked at. I am unable to convert the below line of code to its equavalent vb.net code. could some one please help me with this? static public List<RoleData> GetRoles() { return GetRoles(null, false); }
8
1447
by: Oliver Bleckmann | last post by:
i want to create a object of class Hotel, which own a variable number of objects of the class Zimmer (~ rooms). accessed in main like this Hotel hotel("name",9); std::cout << hotel.zimmers->getZimmerZahl(); //(~ hotel.rooms->getRoomNumber) well, i don't know where to put the pointer declaration. Zimmer* zimmers; // Array als Pointer auf...
4
6992
by: josh | last post by:
Hi, which is the way to init a static array inside a constructor function? i.e. if I had to init ---> int x = {100,200}
4
3693
by: Jess | last post by:
Hello, I tried several books to find out the details of object initialization. Unfortunately, I'm still confused by two specific concepts, namely default-initialization and value-initialization. I think default-init calls default constructor for class objects and sets garbage values to PODs. Value-init also calls default constructor for...
15
2153
by: Jess | last post by:
Hello, Sometimes declarations are all what we need when we define/declare classes (or functions?), but sometimes we need definitions. I learned that if we define a class (B) that has an object (a_obj) of a class type (A), then we need to define A as well, but if B has a pointer to A, then we only need to forward declare A. I was told this...
10
2088
by: CuTe_Engineer | last post by:
hii, i have cs assignment i tried to solve it but i still have many errors , plzz help mee :"< it`s not cheating becuz i`ve tried & wrote the prog. i just wanna you to show me my mistakes #these are the operations + = , - = , * = , 1/ = only if 0 not in .
7
1951
by: howmanymiles | last post by:
I am currently writing a program which works out student marks and outputs the passes and fails two files. I have written some code but it does seem work as no names are displayed and according to this everybody has passed. My switch function is working, so why dont the other functions work? I have been creating new constructors, adding to the...
0
7270
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7178
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7563
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7125
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7543
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5703
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
4757
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
1612
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
813
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.