Results 1 to 7 of 7

Thread: [Resolved] [1.0/1.1] Static initialization

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Jan 2005
    Location
    Cebu
    Posts
    607

    [Resolved] [1.0/1.1] Static initialization

    Is there any static block for C#, can't seem to find a bit.

    Code:
    private static IList categories = new ArrayList();
    		static 
    		{
    			categories.Add("News and Features");
    			categories.Add("Kababayan Edition");
    			categories.Add("Hometown");
    			categories.Add("Owning a Home");
    		}
    ?
    Last edited by nebulom; Jul 18th, 2006 at 02:26 AM.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [1.0/1.1] Static initialization

    Where does member initialisation get performed? In a constructor. Where does static member initialisation get performed? You guessed it. You should read this MSDN topic first.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Jan 2005
    Location
    Cebu
    Posts
    607

    Re: [1.0/1.1] Static initialization

    Kewl. Thanks a bunch. I was just referring to java syntax and never tried to search. My bad. Again, thanks!

  4. #4
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: [1.0/1.1] Static initialization

    Quote Originally Posted by jmcilhinney
    Where does member initialisation get performed? In a constructor. Where does static member initialisation get performed? You guessed it. You should read this MSDN topic first.
    Actually this code isn't "Member Initialization". in Java Initializers run before constructors, only once.
    They are used for static and non-static members
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [Resolved] [1.0/1.1] Static initialization

    My language was somewhat inaccurate. I should just said "initialisation" or "object initialisation". The first line of nebulom's code is initialisation of a member and it is executed before constructor code in C# also.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: [Resolved] [1.0/1.1] Static initialization

    I'm not sure but I guess the first line doesn't have anything to do with the question.
    I believe he was asking about
    Code:
    static 
    		{
    			categories.Add("News and Features");
    			categories.Add("Kababayan Edition");
    			categories.Add("Hometown");
    			categories.Add("Owning a Home");
    		}
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [Resolved] [1.0/1.1] Static initialization

    I realise that. I'm saying that the first line was initialising the member, i.e. assigning an initial value to the member variable, while the rest of the code would more correctly be termed "object initialisation", i.e. it is setting the new object being created to its initial state. I should have said in post #2 that "object initialisation" is done in a constructor. Initialisation of members can occur in a constructor or on the same line as their declarations, in which case it is executed before any constructors.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width