Question

I have made this program in c# which implements IComparable interface to compare the name of vehicles and sort them alphabetically.The code compiled successfully but while executing it is giving me StackOverFlowExecption.This is my code-

  class Vehicle:IComparable
  {
    private string vehiclename 
    { 
        get 
        { return vehiclename; 
        }
        set
        {
            vehiclename = value;
        }
    }


    public Vehicle(string name)
    {
        vehiclename = name;
    }


    int IComparable.CompareTo(Object obj)
    {
        Vehicle temp = (Vehicle)obj;
        return string.Compare(this.vehiclename, temp.vehiclename);
    }

    static void Main(string[] args)
    {
        Vehicle[] myvehicles = new Vehicle[5];
        myvehicles[0] = new Vehicle("Honda City");
        myvehicles[1] = new Vehicle("Nano");
        myvehicles[2] = new Vehicle("Desire");
        myvehicles[3] = new Vehicle("Santro");
        myvehicles[4] = new Vehicle("Nissan");

        Console.WriteLine("Unordered List of vehicles:");
        foreach (Vehicle v in myvehicles)
            Console.WriteLine(myvehicles);

        Array.Sort(myvehicles);

        Console.WriteLine("ordered List of vehicles:");
        foreach (Vehicle v in myvehicles)
            Console.WriteLine(myvehicles);

    }
}

What is the cause of this exception and how can I resolve it?

Was it helpful?

Solution

Your get and set invoke themselves:

private string vehiclename 
{ 
    get 
    { return vehiclename; 
    }
    set
    {
        vehiclename = value;
    }
}

So accessing this property (for get or set) will cause an overflow to occur.

I suspect you either want an auto-implemented property:

private string vehiclename 
{ 
    get;
    set;
}

Or to provide your own backing field:

private string _vehiclename;
private string vehiclename 
{ 
    get 
    { return _vehiclename; 
    }
    set
    {
        _vehiclename = value;
    }
}

Or possibly, you don't want a property at all (private properties are quite rare) and just wanted a field:

private string vehiclename;

OTHER TIPS

private string vehiclename 
    { 
        get 
        { 
            return vehiclename; 
        }
        set
        {
            vehiclename = value;
        }
    }

You are creating a loop with this code!

When you set a value to the property vehiclename, "value" is assigned to vehiclename, "value" is assigned to vehiclename, "value" is assigned to vehiclename...

This can be sloved by renaming the Property like this:

     private string Vehiclename 
            { 
                get 
                { 
                    return vehiclename; 
                }
                set
                {
                    vehiclename = value;
                }
            }
    string vehiclename = string.Empty;

Your property causes the stack overflow exception. Lets have a look:

private string vehiclename 
{ 
    get 
    { return vehiclename; 
    }
    set
    {
        vehiclename = value;
    }
}

Upon calling the setter of vehiclename, it assigns the value to the property vehiclename which calls the setter .... This is of course a stack overflow. When using properties, you should stick to auto implemented properties or a valid backing field.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top