Click here to Skip to main content
15,921,463 members

Welcome to the Lounge

   

For discussing anything related to a software developer's life but is not for programming questions. Got a programming question?

The Lounge is rated Safe For Work. If you're about to post something inappropriate for a shared office environment, then don't post it. No ads, no abuse, and no programming questions. Trolling, (political, climate, religious or whatever) will result in your account being removed.

 
GeneralRe: MQOTD Pin
Pirate Guy16-Apr-15 22:38
Pirate Guy16-Apr-15 22:38 
GeneralRe: MQOTD Pin
Nagy Vilmos16-Apr-15 22:31
professionalNagy Vilmos16-Apr-15 22:31 
GeneralRe: MQOTD Pin
chriselst16-Apr-15 22:34
professionalchriselst16-Apr-15 22:34 
GeneralRe: MQOTD Pin
Corporal Agarn17-Apr-15 3:05
professionalCorporal Agarn17-Apr-15 3:05 
GeneralRe: MQOTD Pin
Stefto17-Apr-15 3:51
professionalStefto17-Apr-15 3:51 
GeneralXKCD OTD PinPopular
OriginalGriff16-Apr-15 20:22
mveOriginalGriff16-Apr-15 20:22 
GeneralRe: XKCD OTD Pin
Nagy Vilmos16-Apr-15 22:03
professionalNagy Vilmos16-Apr-15 22:03 
GeneralFriday Programming Quiz PinPopular
PIEBALDconsult16-Apr-15 19:50
mvePIEBALDconsult16-Apr-15 19:50 
Guess the output:

C#
namespace Quiz
{
  public class Base
  {
    public static string Name { get ; protected set ; }

    static Base() { Name = "I am Base" ; }
  }

  public class Derived : Base
  {
    static Derived() { Name = "I am Derived" ; }
  }

  public static class Quiz
  {
    public static void Main()
    {
      System.Console.WriteLine ( Base.Name ) ;
      System.Console.WriteLine ( Derived.Name ) ;

      Base b = new Derived() ;
      System.Console.WriteLine ( Derived.Name ) ;
    }
  }
}


This issue bit me this week when I refactored some code.

Originally I had a base class (with some static members) and some derived classes that set the static values as needed. (This has worked just fine for more than a year.)
This week, while refactoring, I decided that the static and instance parts should be separated (they really should), so I split them. A few days later I realized that the static parts weren't behaving as expected. D'Oh! | :doh:

The problem was that after splitting the instance parts away, I no longer instantiate the class that contains the static parts and the static constructors in the derived classes never get called! OMG | :OMG:


My solution was to eliminate the use of static and to have a normal class that I instantiate with the appropriate values. This is a much better technique for other reasons as well.


The following two pages were no help:

https://msdn.microsoft.com/en-us/library/k9x6w0hc(v=vs.120).aspx[^]

http://csharpindepth.com/Articles/General/BeforeFieldInit.aspx[^]
GeneralRe: Friday Programming Quiz Pin
Agent__00716-Apr-15 20:23
professionalAgent__00716-Apr-15 20:23 
JokeRe: Friday Programming Quiz Pin
RUs12316-Apr-15 21:33
RUs12316-Apr-15 21:33 
JokeRe: Friday Programming Quiz Pin
Johnny J.16-Apr-15 21:38
professionalJohnny J.16-Apr-15 21:38 
GeneralRe: Friday Programming Quiz Pin
Nagy Vilmos16-Apr-15 22:09
professionalNagy Vilmos16-Apr-15 22:09 
GeneralRe: Friday Programming Quiz Pin
Johnny J.16-Apr-15 23:06
professionalJohnny J.16-Apr-15 23:06 
JokeRe: Friday Programming Quiz Pin
Stefto17-Apr-15 3:52
professionalStefto17-Apr-15 3:52 
QuestionThe Friday Post!™ Pin
Vikram A Punathambekar16-Apr-15 18:51
Vikram A Punathambekar16-Apr-15 18:51 
AnswerRe: The Friday Post!™ Pin
Roger Wright16-Apr-15 19:16
professionalRoger Wright16-Apr-15 19:16 
AnswerRe: The Friday Post!™ Pin
Brady Kelly16-Apr-15 19:30
Brady Kelly16-Apr-15 19:30 
GeneralRe: The Friday Post!™ Pin
Roger Wright16-Apr-15 19:44
professionalRoger Wright16-Apr-15 19:44 
GeneralRe: The Friday Post!™ Pin
Mycroft Holmes16-Apr-15 22:39
professionalMycroft Holmes16-Apr-15 22:39 
AnswerRe: The Friday Post!™ Pin
King Fisher16-Apr-15 19:34
professionalKing Fisher16-Apr-15 19:34 
GeneralRe: The Friday Post!™ Pin
Mycroft Holmes16-Apr-15 22:40
professionalMycroft Holmes16-Apr-15 22:40 
GeneralRe: The Friday Post!™ Pin
King Fisher16-Apr-15 23:29
professionalKing Fisher16-Apr-15 23:29 
AnswerRe: The Friday Post!™ Pin
HobbyProggy16-Apr-15 20:11
professionalHobbyProggy16-Apr-15 20:11 
AnswerRe: The Friday Post!™ Pin
Stefto16-Apr-15 20:51
professionalStefto16-Apr-15 20:51 
AnswerRe: The Friday Post!™ Pin
Dinesh.V.Kumar16-Apr-15 21:32
Dinesh.V.Kumar16-Apr-15 21:32 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.