473,545 Members | 2,021 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 "dataInitialize d"
boolean to be true, which ever access to the data could check (via
properties).

What do you think?

Titan

Mar 29 '07 #1
2 1664
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.co m

<ti***********@ gmail.comwrote in message
news:11******** **************@ n59g2000hsh.goo glegroups.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 "dataInitialize d"
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
3137
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
3860
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
3772
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
6994
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
3695
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
2154
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
2091
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
1954
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
7475
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
7921
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
7437
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
5982
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...
1
5343
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4958
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
3465
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3446
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
720
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.