Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VS2008 error with recursive C# class

I have a class within VS2008 written in C#. The class is recursive.

When I nan instance of this class and view it whilst debugging, VS2008 stalls for a few seconds and then the debug session exits.

Any ideas what the problem might be.

The class is

public class TextSection
{
    private bool used;
    private string id;
    private HL7V3_CD code;
    private string title;
    private string text;

    public List<TextSection> section;


    public TextSection()
    {
        used = false;
        section = new List<TextSection>();
    }

    public bool Used
    {
        get { return used; }
    }

    public string Title
    {
        get { return title; }
        set
        {
            used = true;
            title = value;
        }
    }

    public string Text
    {
        get { return text; }
        set
        {
            used = true;
            text = value;
        }
    }

    public string Id
    {
        get { return id; }
        set
        {
            used = true;
            id = value;
        }
    }

    public HL7V3_CD Code
    {
        get { return Code; }
        set
        {
            used = true;
            code = value;
        }
    }
}

When debugging a screenshot of VS2008 before it exits is shown here

like image 636
BENBUN Coder Avatar asked Aug 10 '26 14:08

BENBUN Coder


2 Answers

The problem is this property

public HL7V3_CD Code
{
    get { return Code; }
    set
    {
        used = true;
        code = value;
    }
}

It will generate a StackOverflowException, when the debugger tries to get the value from property Code, cause Code calls itself instead of returning the value of a variable

like image 156
Jehof Avatar answered Aug 13 '26 04:08

Jehof


You should change this segment

public HL7V3_CD Code
{
    get { return Code; }
    set
    {
        used = true;
        code = value;
    }
}

to

public HL7V3_CD Code
{
    get { return code; }
    set
    {
        used = true;
        code = value;
    }
}
like image 36
Kiril Popov Avatar answered Aug 13 '26 03:08

Kiril Popov