question from C# novice

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jose

    question from C# novice

    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 DisplacementUni ts { 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.StackOv erflowException '
    }

    }
    }

    class DisplacementVar iable : Variable
    {
    public override int Unit
    {
    get
    {
    return Unit;
    }
    set
    {
    Console.WriteLi ne("set");
    }
    }

    public DisplacementVar iable()
    {
    Unit = (int)Displaceme ntUnits.Feet;
    Value = 0;
    }
    public DisplacementVar iable(double value, int unit)
    {
    Unit = unit;
    Value = value;
    }
    }

    class Settings
    {
    double someVar1;
    string someVar2;


    static void Main(string[] args)
    {
    DisplacementVar iable x = new DisplacementVar iable(0,0);
    Console.WriteLi ne("in main..");
    Console.ReadLin e();
    }
    }
    }

    I'm using VS2005 beta1.
  • SB

    #2
    Re: question from C# novice

    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@discussio ns.microsoft.co m> wrote in message
    news:BE5B066F-6499-45C3-8E4A-24554CC9442B@mi crosoft.com...[color=blue]
    > 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 DisplacementUni ts { 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.StackOv erflowException '
    > }
    >
    > }
    > }
    >
    > class DisplacementVar iable : Variable
    > {
    > public override int Unit
    > {
    > get
    > {
    > return Unit;
    > }
    > set
    > {
    > Console.WriteLi ne("set");
    > }
    > }
    >
    > public DisplacementVar iable()
    > {
    > Unit = (int)Displaceme ntUnits.Feet;
    > Value = 0;
    > }
    > public DisplacementVar iable(double value, int unit)
    > {
    > Unit = unit;
    > Value = value;
    > }
    > }
    >
    > class Settings
    > {
    > double someVar1;
    > string someVar2;
    >
    >
    > static void Main(string[] args)
    > {
    > DisplacementVar iable x = new DisplacementVar iable(0,0);
    > Console.WriteLi ne("in main..");
    > Console.ReadLin e();
    > }
    > }
    > }
    >
    > I'm using VS2005 beta1.[/color]


    Comment

    • Jose

      #3
      Re: question from C# novice

      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:
      [color=blue]
      > 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@discussio ns.microsoft.co m> wrote in message
      > news:BE5B066F-6499-45C3-8E4A-24554CC9442B@mi crosoft.com...[color=green]
      > > 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 DisplacementUni ts { 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.StackOv erflowException '
      > > }
      > >
      > > }
      > > }
      > >
      > > class DisplacementVar iable : Variable
      > > {
      > > public override int Unit
      > > {
      > > get
      > > {
      > > return Unit;
      > > }
      > > set
      > > {
      > > Console.WriteLi ne("set");
      > > }
      > > }
      > >
      > > public DisplacementVar iable()
      > > {
      > > Unit = (int)Displaceme ntUnits.Feet;
      > > Value = 0;
      > > }
      > > public DisplacementVar iable(double value, int unit)
      > > {
      > > Unit = unit;
      > > Value = value;
      > > }
      > > }
      > >
      > > class Settings
      > > {
      > > double someVar1;
      > > string someVar2;
      > >
      > >
      > > static void Main(string[] args)
      > > {
      > > DisplacementVar iable x = new DisplacementVar iable(0,0);
      > > Console.WriteLi ne("in main..");
      > > Console.ReadLin e();
      > > }
      > > }
      > > }
      > >
      > > I'm using VS2005 beta1.[/color]
      >
      >
      >[/color]

      Comment

      • Patrice

        #4
        Re: question from C# novice

        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@discussio ns.microsoft.co m> a écrit dans le message de
        news:8F9FE381-ADA8-42D0-B308-E5275C26E8ED@mi crosoft.com...[color=blue]
        > 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[/color]
        reserved.[color=blue]
        >
        > "SB" wrote:
        >[color=green]
        > > Your problem is that inside your setter, you are calling your setter
        > > again....which will cause a stack overflow as you have seen :) Where[/color][/color]
        is[color=blue][color=green]
        > > your variable to hold Value? That is what you should be[/color][/color]
        setting/getting.[color=blue][color=green]
        > >
        > > HTH,
        > > -sb
        > >
        > > "Jose" <Jose@discussio ns.microsoft.co m> wrote in message
        > > news:BE5B066F-6499-45C3-8E4A-24554CC9442B@mi crosoft.com...[color=darkred]
        > > > When I try to compile the code below I receive a first chance[/color][/color][/color]
        exception.[color=blue][color=green][color=darkred]
        > > > I've
        > > > noticed from searching on the web that some people choose to ignore[/color][/color][/color]
        first[color=blue][color=green][color=darkred]
        > > > chance exceptions. Is this common practice? Do you guys see anything[/color][/color][/color]
        wrong[color=blue][color=green][color=darkred]
        > > > with my code or should I ignore the exception it generates?
        > > >
        > > > <Using directives here>
        > > > namespace Settings
        > > > {
        > > >
        > > > enum DisplacementUni ts { 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.StackOv erflowException '
        > > > }
        > > >
        > > > }
        > > > }
        > > >
        > > > class DisplacementVar iable : Variable
        > > > {
        > > > public override int Unit
        > > > {
        > > > get
        > > > {
        > > > return Unit;
        > > > }
        > > > set
        > > > {
        > > > Console.WriteLi ne("set");
        > > > }
        > > > }
        > > >
        > > > public DisplacementVar iable()
        > > > {
        > > > Unit = (int)Displaceme ntUnits.Feet;
        > > > Value = 0;
        > > > }
        > > > public DisplacementVar iable(double value, int unit)
        > > > {
        > > > Unit = unit;
        > > > Value = value;
        > > > }
        > > > }
        > > >
        > > > class Settings
        > > > {
        > > > double someVar1;
        > > > string someVar2;
        > > >
        > > >
        > > > static void Main(string[] args)
        > > > {
        > > > DisplacementVar iable x = new DisplacementVar iable(0,0);
        > > > Console.WriteLi ne("in main..");
        > > > Console.ReadLin e();
        > > > }
        > > > }
        > > > }
        > > >
        > > > I'm using VS2005 beta1.[/color]
        > >
        > >
        > >[/color][/color]


        Comment

        Working...