0
votes

trivial mistakes are often most hard to locate ! done this many times but don't know why its throwing his error

I need to get employee firstname and lastname concatenated in a property FullName.So that the display member of a combobox can be set to "FullName".

For this i simply created another partial class corresponding to the Employee class generated in my data model as below

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Objects.DataClasses;

namespace IGarage.DAL
{
    public partial class Employee : EntityObject
    {
        public string FullName
        {
            get
            {
                return FullName;
            }
            set
            {
                value = this.FirstName + " " + this.LastName;
            }
        }
    }
}

in a file named Employee.cs

But when i query the DB related to Employee it throws following error :

Exception !!

Also when i explore the problem i see this

This ! please advice.

2
I would suspect that the LINQ call is somehow entering an infinite loop, though I'm not sure how....Tim
me too, but can't figure it out where !tariq
In the getter of FullName, you call the getter of FullName again, which causes the infinite recursion.Daniel Sklenitzka
@DanielSklenitzka elaborate pleasetariq
@taniq - FullName calls the FullName property, which calls the FullName property, which calls theFullName property....Tim

2 Answers

4
votes

As stated in the comments, your implementation doesn't work because the getter of the FullName property just calls itself again, which causes the infinite recursion. Also, modifying the value variable in the setter doesn't do what you want. It doesn't store the string anywhere (except the local variable which goes out of scope at the end of the setter).

From what I can tell, you want a read-only property with no setter at all:

    public string FullName
    {
        get
        {
            return this.FirstName + " " + this.LastName;
        }
    }
0
votes
public string FullName
    {
        get
        {
            return FullName;
        }
}

here you call the getter of the property, which getter you are implementing, which causes infinite recursion. Btw. ask yourself if you really need a setter on this property as the FullName is a concat of a first name and a last name. Personally i would have done it that way:

  public string FullName
    {
        get
        {
            this.FirstName + " " + this.LastName;
        }
}