Dave

Dave

  • NA
  • 161
  • 0

System.StackOverflowException

Jan 16 2009 8:41 PM
Hi there,

I have been trying to debug the following code which was set out in a book:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace HowToCreateUDTs
{
class Program
{
static void Main(string[] args)
{
Cycle degrees = new Cycle(0, 359);

for(int i = 0; i <= 8; i++)
{
degrees += 90;
Console.WriteLine("degrees = {0}", degrees);
}
}

}

struct Cycle
{
// private fields
int _val, _min, _max;

// constructor
public Cycle(int min, int max)
{
_val = min;
_min = min;
_max = max;
}

public int Value
{
get { return _val; }
set
{
if (value > _max)
this.Value = value - _max + _min - 1;
else
{
if (value < _max)
this.Value = _min - value + _max - 1;
else
_val = value;
}
}
}

public override string ToString()
{
return Value.ToString();
}

public int ToInteger()
{
return Value;
}

public static Cycle operator +(Cycle arg1, int arg2)
{
arg1.Value += arg2;
return arg1;
}

public static Cycle operator -(Cycle arg1, int arg2)
{
arg1.Value -= arg2;
return arg1;
}
}
}


At the first iterarion of the loop, the exception thrown is:
System.StackOverflowException was unhandled
and it occurs at the first addition operation.

Any help would be appreciated. It has me baffled.

Answers (4)