Re: question from C# novice
From: Patrice (nobody_at_nowhere.com)
Date: 03/17/05
- Next message: John E.: "Strong name assignment to a circular reference"
- Previous message: John E.: "Strong name assignment to a circular reference"
- In reply to: Jose: "Re: question from C# novice"
- Messages sorted by: [ date ] [ thread ]
Date: Thu, 17 Mar 2005 20:31:24 +0100
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" <Jose@discussions.microsoft.com> a écrit dans le message de
news:8F9FE381-ADA8-42D0-B308-E5275C26E8ED@microsoft.com...
> 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" wrote:
>
> > 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" <Jose@discussions.microsoft.com> wrote in message
> > news:BE5B066F-6499-45C3-8E4A-24554CC9442B@microsoft.com...
> > > 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.
> >
> >
> >
- Next message: John E.: "Strong name assignment to a circular reference"
- Previous message: John E.: "Strong name assignment to a circular reference"
- In reply to: Jose: "Re: question from C# novice"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|