Thread: construction order

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    1,579

    construction order

    Hello everyone,


    For the following code, it is correct to say that,

    1. obj1 will be instialized when .NET Runtime loads class Foo and before any instance is created?

    2. obj2 will be created each time we create an instance of Foo, and obj2 will be called before constructor?

    My question is whether my understanding (1) and (2) are correct?

    Code:
    class Foo
    {
        static private object obj1 = new object();
        private object obj2 = new object();
        public Foo()
        {
        }
    }

    thanks in advance,
    George

  2. #2
    Registered User
    Join Date
    Jun 2003
    Posts
    129
    I know (2) is incorrect. There is no order. It's just a list of items in the class in no particular order other than which you typed first. It's correct that obj2 will be created each time you create an instance of Foo().

    And correct me if i'm wrong, but for the static obj1 to work, the class must be static, otherwise it'll just act like obj2.
    He who asks is a fool for five minutes, but he who does not ask remains a fool forever.

    The fool wonders, the wise man asks. - Benjamin Disraeli

    There are no foolish questions and no man becomes a fool until he has stopped asking questions. Charles Steinmetz

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks DanFraser,


    Your answer is clear.

    Quote Originally Posted by DanFraser View Post
    I know (2) is incorrect. There is no order. It's just a list of items in the class in no particular order other than which you typed first. It's correct that obj2 will be created each time you create an instance of Foo().

    And correct me if i'm wrong, but for the static obj1 to work, the class must be static, otherwise it'll just act like obj2.

    regards,
    George

  4. #4
    Registered User
    Join Date
    Apr 2008
    Location
    USA
    Posts
    24

    Wink

    You could also use conditionals like if - else and switch cases in the Foo method to control the instantiation of objects as well. In C# an object can be created from within a method albeit it should be derived from a virtual or abstract type.

    Something fun to think about...

  5. #5
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks TheRaven,


    I am interested in "albeit it should be derived from a virtual or abstract type". Could you show some pseudo code please or more description (so that I can write some code)?

    Quote Originally Posted by TheRaven View Post
    You could also use conditionals like if - else and switch cases in the Foo method to control the instantiation of objects as well. In C# an object can be created from within a method albeit it should be derived from a virtual or abstract type.

    Something fun to think about...

    regards,
    George

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Laplace Expansion
    By Leojeen in forum C Programming
    Replies: 7
    Last Post: 10-28-2008, 11:26 PM
  2. Best way to have an list of indices which changes order
    By laertius in forum C Programming
    Replies: 5
    Last Post: 09-29-2008, 12:45 AM
  3. Embedded SQL Order By
    By cjohnman in forum C Programming
    Replies: 12
    Last Post: 04-15-2008, 03:45 PM
  4. How do you order your game code?
    By Queatrix in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 02-05-2006, 06:26 PM
  5. what is the significance of low order and high order bits
    By Shadow12345 in forum Windows Programming
    Replies: 1
    Last Post: 11-16-2002, 11:46 AM