question from C# novice

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

When I try to compile the code below I receive a first chance exception. I've
noticed from searching on the web that some people choose to ignore first
chance exceptions. Is this common practice? Do you guys see anything wrong
with my code or should I ignore the exception it generates?

<Using directives here>
namespace Settings
{

enum DisplacementUnits { Feet, Kilofeet, Meters, Kilometers, NauticalMiles };

abstract class Variable
{
public abstract int Unit
{
get;
set;
}
public double Value //Using another name doesn't help
{
get
{
return Value;
}
set
{
Value = value; //Generates a A first chance exception of type
'System.StackOverflowException'
}

}
}

class DisplacementVariable : Variable
{
public override int Unit
{
get
{
return Unit;
}
set
{
Console.WriteLine("set");
}
}

public DisplacementVariable()
{
Unit = (int)DisplacementUnits.Feet;
Value = 0;
}
public DisplacementVariable(double value, int unit)
{
Unit = unit;
Value = value;
}
}

class Settings
{
double someVar1;
string someVar2;


static void Main(string[] args)
{
DisplacementVariable x = new DisplacementVariable(0,0);
Console.WriteLine("in main..");
Console.ReadLine();
}
}
}

I'm using VS2005 beta1.
 
Your problem is that inside your setter, you are calling your setter
again....which will cause a stack overflow as you have seen :) Where is
your variable to hold Value? That is what you should be setting/getting.

HTH,
-sb
 
I'm confused - are you saying that I can't have a variable named Value? I
know "value" is a special word but I didn't know that Value was also reserved.

SB said:
Your problem is that inside your setter, you are calling your setter
again....which will cause a stack overflow as you have seen :) Where is
your variable to hold Value? That is what you should be setting/getting.

HTH,
-sb

Jose said:
When I try to compile the code below I receive a first chance exception.
I've
noticed from searching on the web that some people choose to ignore first
chance exceptions. Is this common practice? Do you guys see anything wrong
with my code or should I ignore the exception it generates?

<Using directives here>
namespace Settings
{

enum DisplacementUnits { Feet, Kilofeet, Meters, Kilometers,
NauticalMiles };

abstract class Variable
{
public abstract int Unit
{
get;
set;
}
public double Value //Using another name doesn't help
{
get
{
return Value;
}
set
{
Value = value; //Generates a A first chance exception of type
'System.StackOverflowException'
}

}
}

class DisplacementVariable : Variable
{
public override int Unit
{
get
{
return Unit;
}
set
{
Console.WriteLine("set");
}
}

public DisplacementVariable()
{
Unit = (int)DisplacementUnits.Feet;
Value = 0;
}
public DisplacementVariable(double value, int unit)
{
Unit = unit;
Value = value;
}
}

class Settings
{
double someVar1;
string someVar2;


static void Main(string[] args)
{
DisplacementVariable x = new DisplacementVariable(0,0);
Console.WriteLine("in main..");
Console.ReadLine();
}
}
}

I'm using VS2005 beta1.
 
public double Value //Using another name doesn't help
{
get
{
return Value;
}
set
{
Value = value; // Calls itself until it crashes...
}

Generally the get/set just updates a private variable. Here you are using
the name of the property that is you are calling again the "set" that calls
again the "set" and so on until it crashes...

Patrice


-

Jose said:
I'm confused - are you saying that I can't have a variable named Value? I
know "value" is a special word but I didn't know that Value was also reserved.

SB said:
Your problem is that inside your setter, you are calling your setter
again....which will cause a stack overflow as you have seen :) Where is
your variable to hold Value? That is what you should be setting/getting.

HTH,
-sb

Jose said:
When I try to compile the code below I receive a first chance exception.
I've
noticed from searching on the web that some people choose to ignore first
chance exceptions. Is this common practice? Do you guys see anything wrong
with my code or should I ignore the exception it generates?

<Using directives here>
namespace Settings
{

enum DisplacementUnits { Feet, Kilofeet, Meters, Kilometers,
NauticalMiles };

abstract class Variable
{
public abstract int Unit
{
get;
set;
}
public double Value //Using another name doesn't help
{
get
{
return Value;
}
set
{
Value = value; //Generates a A first chance exception of type
'System.StackOverflowException'
}

}
}

class DisplacementVariable : Variable
{
public override int Unit
{
get
{
return Unit;
}
set
{
Console.WriteLine("set");
}
}

public DisplacementVariable()
{
Unit = (int)DisplacementUnits.Feet;
Value = 0;
}
public DisplacementVariable(double value, int unit)
{
Unit = unit;
Value = value;
}
}

class Settings
{
double someVar1;
string someVar2;


static void Main(string[] args)
{
DisplacementVariable x = new DisplacementVariable(0,0);
Console.WriteLine("in main..");
Console.ReadLine();
}
}
}

I'm using VS2005 beta1.
 
Back
Top