Windows Vista Forums

Simple class with self-referencing member fail to run: StackOverflowException

  1. #1


    pedestrian via DotNetMonster.com Guest

    Simple class with self-referencing member fail to run: StackOverflowException

    I get this from a book on C#. I tried to compile and run it but it throw
    StackOverflowException.
    What is wrong with the self-referencing member?

    Thanks a lot!

    class Tire
    {
    public const Tire GoodStone = new Tire(90);
    public const Tire FireYear = new Tire(100);

    public int manufactureID;
    public Tire()
    {
    Console.WriteLine("Default ctor of Tire.");
    }
    public Tire(int ID)
    {
    Console.WriteLine("Custom ctor of Tire...");
    manufactureID = ID;
    }
    }

    class Program
    {
    static void Main(string[] args)
    {
    Tire t = new Tire();

    Console.ReadLine();
    }
    }

    --
    Warmest Regards,
    Pedestrian

    Message posted via DotNetMonster.com
    http://www.dotnetmonster.com/Uwe/For...neral/200811/1


      My System SpecsSystem Spec

  2.   


  3. #2


    Family Tree Mike Guest

    Re: Simple class with self-referencing member fail to run: StackOverflowException

    "pedestrian via DotNetMonster.com" <u16758@xxxxxx> wrote in message
    news:8dcd38cf3ca82@xxxxxx

    >I get this from a book on C#. I tried to compile and run it but it throw
    > StackOverflowException.
    > What is wrong with the self-referencing member?
    >
    > Thanks a lot!
    >
    > class Tire
    > {
    > public const Tire GoodStone = new Tire(90);
    > public const Tire FireYear = new Tire(100);
    >
    > public int manufactureID;
    > public Tire()
    > {
    > Console.WriteLine("Default ctor of Tire.");
    > }
    > public Tire(int ID)
    > {
    > Console.WriteLine("Custom ctor of Tire...");
    > manufactureID = ID;
    > }
    > }
    >
    > class Program
    > {
    > static void Main(string[] args)
    > {
    > Tire t = new Tire();
    >
    > Console.ReadLine();
    > }
    > }
    >
    > --
    > Warmest Regards,
    > Pedestrian
    >
    > Message posted via DotNetMonster.com
    > http://www.dotnetmonster.com/Uwe/For...neral/200811/1
    >

    I'm guessing you copied something incorrectly.

    Each call to the default or custom ctor in this class creates three
    instances, the one requested, plus FireYear and GoodStone. The problem is
    that the extra two instances then create three instances, each ad nauseum.

    Not knowing what the original text was showing, I cannot recommend how to
    change it other than don't create the FireYear and GoodStone objects in the
    tire class.

    Lastly, perhaps this works in other versions of .Net, but in VS 2008/C# 3.0,
    you cannot declare the following:

    public const Tire FireYear = new Tire(90);

    Objects cannot be constant.

    Mike


      My System SpecsSystem Spec

  4. #3


    pedestrian via DotNetMonster.com Guest

    Re: Simple class with self-referencing member fail to run: StackOverflowException

    Oops I place the wrong code for the question,
    you are right, constant cannot be objects... Thanks!

    What I mean should be readonly instead of const:

    The following still don't work unless there are declared as static readonly...

    any explanation ?

    Thanks...

    class Tire
    {
    public readonly Tire GoodStone = new Tire(90);
    public readonly Tire FireYear = new Tire(100);

    public int manufactureID;
    public Tire()
    {
    Console.WriteLine("Default ctor of Tire.");
    }
    public Tire(int ID)
    {
    Console.WriteLine("Custom ctor of Tire...");
    manufactureID = ID;
    }
    }

    class Program
    {
    static void Main(string[] args)
    {
    Tire t = new Tire();

    Console.ReadLine();
    }
    }



    Family Tree Mike wrote:

    >>I get this from a book on C#. I tried to compile and run it but it throw
    >> StackOverflowException.
    >[quoted text clipped - 28 lines]

    >> }
    >> }
    >
    >I'm guessing you copied something incorrectly.
    >
    >Each call to the default or custom ctor in this class creates three
    >instances, the one requested, plus FireYear and GoodStone. The problem is
    >that the extra two instances then create three instances, each ad nauseum.
    >
    >Not knowing what the original text was showing, I cannot recommend how to
    >change it other than don't create the FireYear and GoodStone objects in the
    >tire class.
    >
    >Lastly, perhaps this works in other versions of .Net, but in VS 2008/C# 3.0,
    >you cannot declare the following:
    >
    >public const Tire FireYear = new Tire(90);
    >
    >Objects cannot be constant.
    >
    >Mike
    --
    Warmest Regards,
    Pedestrian

    Message posted via DotNetMonster.com
    http://www.dotnetmonster.com/Uwe/For...neral/200811/1


      My System SpecsSystem Spec

  5. #4


    Family Tree Mike Guest

    Re: Simple class with self-referencing member fail to run: StackOverflowException

    "pedestrian via DotNetMonster.com" <u16758@xxxxxx> wrote in message
    news:8dd19ae30ec54@xxxxxx

    > Oops I place the wrong code for the question,
    > you are right, constant cannot be objects... Thanks!
    >
    > What I mean should be readonly instead of const:
    >
    > The following still don't work unless there are declared as static
    > readonly...
    >
    > any explanation ?
    >
    > Thanks...
    By making the FireYear and GoodStone objects static, there is no creation of
    them each call to either of the constructors. This means that when you call
    the constructor, there is only one instance created, as a static member is
    created for the class, not for each instance. This is why the stack
    overflow does not happen, as you have eliminated the recursion issue.

    Mike


      My System SpecsSystem Spec

  6. #5


    pedestrian via DotNetMonster.com Guest

    Re: Simple class with self-referencing member fail to run: StackOverflowException

    Yes.... that's the reason static fields works....

    however, about the recursion part, it seems that the constructors
    were not called, at least I didn't notice that since the Console.WriteLine
    statement never show the results....

    Family Tree Mike wrote:

    >> Oops I place the wrong code for the question,
    >> you are right, constant cannot be objects... Thanks!
    >[quoted text clipped - 7 lines]

    >>
    >> Thanks...
    >
    >By making the FireYear and GoodStone objects static, there is no creation of
    >them each call to either of the constructors. This means that when you call
    >the constructor, there is only one instance created, as a static member is
    >created for the class, not for each instance. This is why the stack
    >overflow does not happen, as you have eliminated the recursion issue.
    >
    >Mike
    --
    Warmest Regards,
    Pedestrian

    Message posted via DotNetMonster.com
    http://www.dotnetmonster.com/Uwe/For...neral/200811/1


      My System SpecsSystem Spec

  7. #6


    Family Tree Mike Guest

    Re: Simple class with self-referencing member fail to run: StackOverflowException

    "pedestrian via DotNetMonster.com" <u16758@xxxxxx> wrote in message
    news:8dd76d86cdc35@xxxxxx

    > Yes.... that's the reason static fields works....
    >
    > however, about the recursion part, it seems that the constructors
    > were not called, at least I didn't notice that since the Console.WriteLine
    > statement never show the results....
    >
    > Family Tree Mike wrote:

    >>> Oops I place the wrong code for the question,
    >>> you are right, constant cannot be objects... Thanks!
    >>[quoted text clipped - 7 lines]

    >>>
    >>> Thanks...
    >>
    >>By making the FireYear and GoodStone objects static, there is no creation
    >>of
    >>them each call to either of the constructors. This means that when you
    >>call
    >>the constructor, there is only one instance created, as a static member is
    >>created for the class, not for each instance. This is why the stack
    >>overflow does not happen, as you have eliminated the recursion issue.
    >>
    >>Mike
    >
    > --
    > Warmest Regards,
    > Pedestrian
    >
    > Message posted via DotNetMonster.com
    > http://www.dotnetmonster.com/Uwe/For...neral/200811/1
    >

    Fields are initialized before the constructor code executes. That causes
    the recursion with no output.

    --
    Mike


      My System SpecsSystem Spec

  8. #7


    pedestrian via DotNetMonster.com Guest

    Re: Simple class with self-referencing member fail to run: StackOverflowException

    Yes, fields are initialized first...

    however, I expect after that the constructor being executed....
    then only the next recursion level fields being initialized, then the
    ctor called,
    then further next recursion level fields being initialized, then
    the ctor called....

    I suppose in this case, the constructor should at least been called for
    plenty of times before stack overflow exception...

    Thanks for replying...

    Family Tree Mike wrote:

    >
    >Fields are initialized before the constructor code executes. That causes
    >the recursion with no output.
    >
    --
    Warmest Regards,
    Pedestrian

    Message posted via http://www.dotnetmonster.com


      My System SpecsSystem Spec

  9. #8


    Jack Jackson Guest

    Re: Simple class with self-referencing member fail to run: StackOverflowException

    The first instantiation of a Tire attempts to create a new Tire for
    GoodStone. The creation of that Tire attempts to create a new Tire
    for its Goodstone. The creation of that Tire attempts to create a new
    Tire for its Goodstone.

    You can see where this is going. It never gets past initializing
    Goodstone in any instance.

    On Fri, 28 Nov 2008 03:39:35 GMT, "pedestrian via DotNetMonster.com"
    <u16758@xxxxxx> wrote:

    >Yes, fields are initialized first...
    >
    >however, I expect after that the constructor being executed....
    > then only the next recursion level fields being initialized, then the
    >ctor called,
    > then further next recursion level fields being initialized, then
    >the ctor called....
    >
    >I suppose in this case, the constructor should at least been called for
    >plenty of times before stack overflow exception...
    >
    >Thanks for replying...
    >
    >Family Tree Mike wrote:

    >>
    >>Fields are initialized before the constructor code executes. That causes
    >>the recursion with no output.
    >>

      My System SpecsSystem Spec

  10. #9


    pedestrian via DotNetMonster.com Guest

    Re: Simple class with self-referencing member fail to run: StackOverflowException

    Why is the instantiation of a Tire and subsequent Tire instantiation
    didn't call the constructor? I never see the Console.WriteLine text being
    showed...

    I appreciate your explanation.

    Jack Jackson wrote:

    >The first instantiation of a Tire attempts to create a new Tire for
    >GoodStone. The creation of that Tire attempts to create a new Tire
    >for its Goodstone. The creation of that Tire attempts to create a new
    >Tire for its Goodstone.
    >
    >You can see where this is going. It never gets past initializing
    >Goodstone in any instance.
    >

    >>Yes, fields are initialized first...
    >>
    >[quoted text clipped - 11 lines]

    >>>Fields are initialized before the constructor code executes. That causes
    >>>the recursion with no output.
    --
    Warmest Regards,
    Pedestrian

    Message posted via DotNetMonster.com
    http://www.dotnetmonster.com/Uwe/For...neral/200811/1


      My System SpecsSystem Spec

  11. #10


    Family Tree Mike Guest

    Re: Simple class with self-referencing member fail to run: StackOverflowException

    "pedestrian via DotNetMonster.com" <u16758@xxxxxx> wrote in message
    news:8de5f97050c76@xxxxxx

    > Why is the instantiation of a Tire and subsequent Tire instantiation
    > didn't call the constructor? I never see the Console.WriteLine text being
    > showed...
    >
    > I appreciate your explanation.
    >
    > Jack Jackson wrote:

    >>The first instantiation of a Tire attempts to create a new Tire for
    >>GoodStone. The creation of that Tire attempts to create a new Tire
    >>for its Goodstone. The creation of that Tire attempts to create a new
    >>Tire for its Goodstone.
    >>
    >>You can see where this is going. It never gets past initializing
    >>Goodstone in any instance.
    >>

    >>>Yes, fields are initialized first...
    >>>
    >>[quoted text clipped - 11 lines]

    >>>>Fields are initialized before the constructor code executes. That
    >>>>causes
    >>>>the recursion with no output.
    >
    > --
    > Warmest Regards,
    > Pedestrian
    >
    > Message posted via DotNetMonster.com
    > http://www.dotnetmonster.com/Uwe/For...neral/200811/1
    >
    It never gets to the constructor code in any instance because EVERY instance
    that is created is creating two new instances before the constructor is
    called. There is no end condition to the "preconstructor" events in any of
    the objects. I'm not sure that preconstructor is the right word, but it
    basically means the initialization of all the members such as GoodStone and
    FireYear.

    Cycle 1: instance, field1 (GoodStone), field2 (FireYear)
    Cycle 2: field11, field12, field21, field22
    Cycle 3: field111, field112, field121, field122, field211,
    field212, field221, field222
    and so on...

    The original instance constructor (Cycle 1 instance) never executs until all
    the cycles are done. Obviously that won't happen.

    Worse yet, the subsequent constructors (field1, field111, etc...) will not
    execute until the end of the recursion.

    Hope this helps...

    --
    Mike


      My System SpecsSystem Spec

Page 1 of 2 12 LastLast

Simple class with self-referencing member fail to run: StackOverflowException
Similar Threads
Thread Forum
Moving from class C to class B ip address scheme, any tips(DC/DNS) Server General
When a class is both an inherited class of another, and alsoimplements an interface method .NET General
Access a static member on a nested static class. PowerShell
Access a static member on a nested static class. PowerShell
Calling an XPath on a Binding with an XmlNode causes StackOverflowException Avalon