Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 995559
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T06:44:32+00:00 2026-05-16T06:44:32+00:00

Suppose we have a class like: Public Class Question Private Shared _field as Integer

  • 0

Suppose we have a class like:

Public Class Question 

    Private Shared _field as Integer = CrazyIntegersRepository.GetOne()

    ' Some other useful things go here

End Class

And the method GetOne throws an exception… How can we manage that? Is a good practice to rewrite that into a static constructor?
When is the GetOne method going to be executed if we leave it there in the inline _field declaration?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T06:44:32+00:00Added an answer on May 16, 2026 at 6:44 am

    Note: I’m assuming VB works the same as C# here. I’d be surprised if they differed on this.

    If you leave it there (and don’t have a static constructor), it will depend on the version of .NET you’re using. It’s only guaranteed to be run “at some point before the first reference to a static field”. You can even create instances and the type initializer may not run! If you have a static constructor (even an empty one), the type initializer will be run directly before the first reference to any constructor or any static member. (Basically, almost anything you actually do with it will initialize it.)

    The actual observed behaviour has become lazier in .NET 4 compared with .NET 3.5, as I blogged about. Note that this is only talking about the desktop framework; I don’t know what Silverlight or the Compact Framework do.

    If the method can throw an exception, I’d be tempted to do it rather more lazily in the first place, in a method call, maybe caching the result appropriately. That way, the method can let the exception bubble up and the caller can try again later. That’s appropriate if it’s a potentially transient exception you’re considering. If it’s something which indicates the whole system is unusable, it’s fine to let the type initializer fail.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have simple class like: class MyClass { private $_prop; public function getProp()
Suppose I have a C++ class like so: class A { public: A() {
Suppose I have something like this: class A { public B mem; public int
Suppose I have a function defined like this: class Foo() { public: void bar(MyClass*
Suppose I have a class like this: class Owner { private: long m_Id; QString
Suppose you have a simple class like this: class foo{ private: int* mData; int
Let's suppose I have xml like this one: <Server Active=No> <Url>http://some.url</Url> </Server> C# class
Suppose you have this class: public class A { private int number; public setNumber(int
Suppose I have a class like the following: public class Stage { public int
Suppose I have class like class A{ public: A(int a, boost::shared_ptr<int> ptr){ // whatever!

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.