0

I have getter which changes the first letter of a string to capital. But I get StackOverflowException.

namespace ConsoleApplication1
{
    class Program
    {
        class Human
        {     
            public String Name
            {
                get
                {

                    char[] letters = Name.ToCharArray();
                    // upper case the first char
                    letters[0] = char.ToUpper(letters[0]);
                    // return the array made of the new char array
                    return new String(letters);
                    //return Name.First().ToString().ToUpper() + String.Join("", Name.Skip(1));
                }
                set
                {
                
                }
            }

What did I do wrong?

4
  • 6
    Dear lord, where do I begin? Commented May 8, 2013 at 21:16
  • 1
    Give the boy a chance, he might be a beginner struggling with his first lines of code. Commented May 8, 2013 at 21:18
  • 1
    First, your example code won't compile. 2 return statements and an open parenthesis. Second, your getter is referencing itself recursively. You need to set up a private string _Name and do your calculations on it. Commented May 8, 2013 at 21:21
  • Yes is my first line of code in C#. Anyway CLAPTRAP revolution!:P Commented May 9, 2013 at 16:24

5 Answers 5

6

This line char[] letters = Name.ToCharArray(); calls recursively the property public String Name

Sign up to request clarification or add additional context in comments.

Comments

4

Your property Name is calling itself in line char[] letters = Name.ToCharArray();. You will have to use a field instead of the property:

class Human
{     
    private string _name;

    public String Name
    {
        get
        {
            if (_name == null)
                return null;
            if (_name.Length <= 1)
                return _name.ToUpper() + _name.Substring(1);
            return _name.Substring(0, 1).ToUpper() + _name.Substring(1);
        }
        set
        {
            _name = value;
        }
    }
}

I also took the freedom to try to make your function work with the upper casing. Or in my opinion even better:

class Human
{     
    private string _name = "";

    public String Name
    {
        get
        {
            return _name;
        }
        set
        {
            if (String.IsNullOrEmpty(_name))
                _name = value;
            else  
                _name = value.Substring(0, 1).ToUpper() + _value.Substring(1);
        }
    }
}

2 Comments

Move your _name field out of the property and I'll upvote it. :)
@MadHenchbot: Hahaha, that is a nice and stupid mistake! Thanks! :)
2

That's because you are accesing the getter inside the getter.

You should create a backing variable and access that variable instead of the Name:

class Human
    {     
    private string _name = string.Empty;    
    public String Name
        {
            get
            {

                char[] letters = _name.ToCharArray();  // use the backing variable instead of Name
                // upper case the first char
                letters[0] = char.ToUpper(letters[0]);
                // return the array made of the new char array
                return new String(letters);
                //return Name.First().ToString().ToUpper() + String.Join("", Name.Skip(1));

                return new String(;// ToUpperFirstLetter(this.Imie);
            }
            set
            {
                _name = value;
            }
        }

Comments

1

This:

char[] letters = Name.ToCharArray();

When you try to read something from Name, you're using the getter. Thus you've caused an infinite loop.

Comments

0

Here's a working Name method, which makes the first letter of the sentence uppercase, when you get the value.

private string _name;
public string Name
{
    get
    {
        if (string.IsNullOrEmpty(_name))
        {
            return _name;
        }
        var charArray = _name.ToCharArray();
        charArray[0] = char.ToUpper(charArray[0]);
        return new string(charArray);
    }
    set { _name = value; }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.