Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Errors & Exceptions in C#

Errors & Exceptions in C#

Scheduled Pinned Locked Moved C#
csharptutorial
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Anand Amirineni
    wrote on last edited by
    #1

    How to solve "System.StackOverflowException" I am getting this, when I try to set a value to memeber variable. namespace StackOverFLow { /// /// Summary description for Class1. /// class StFlow { /// /// The main entry point for the application. /// [STAThread] static void Main(string[] args) { StFlow STF = new StFlow(); STF.Name = "CodeProject"; string str = STF.Name; } public string Name { get { return Name; } set { Name = value; } } } } Thanks, Candy

    J L 2 Replies Last reply
    0
    • A Anand Amirineni

      How to solve "System.StackOverflowException" I am getting this, when I try to set a value to memeber variable. namespace StackOverFLow { /// /// Summary description for Class1. /// class StFlow { /// /// The main entry point for the application. /// [STAThread] static void Main(string[] args) { StFlow STF = new StFlow(); STF.Name = "CodeProject"; string str = STF.Name; } public string Name { get { return Name; } set { Name = value; } } } } Thanks, Candy

      J Offline
      J Offline
      James T Johnson
      wrote on last edited by
      #2

      The problem is caused because your property is trying to get/set itself. You need a private field to store the value, and the property gets/sets that.

      private string name;
      public string Name {
      get {
      return name;
      }
      set {
      name = value;
      }
      }

      Notice the different in case between name and Name. C# is case sensitive :) HTH, James Sonork ID: 100.11138 - Hasaki "Smile your little smile, take some tea with me awhile. And every day we'll turn another page. Behind our glass we'll sit and look at our ever-open book, One brown mouse sitting in a cage." "One Brown Mouse" from Heavy Horses, Jethro Tull 1978

      1 Reply Last reply
      0
      • A Anand Amirineni

        How to solve "System.StackOverflowException" I am getting this, when I try to set a value to memeber variable. namespace StackOverFLow { /// /// Summary description for Class1. /// class StFlow { /// /// The main entry point for the application. /// [STAThread] static void Main(string[] args) { StFlow STF = new StFlow(); STF.Name = "CodeProject"; string str = STF.Name; } public string Name { get { return Name; } set { Name = value; } } } } Thanks, Candy

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        Every time you access Name within your getter or setter code, you recursively invoking it. It stops only when this call sequence exhaust the call stack. You can check it tracing execution in debugger. There are to possible ways to correct this. First is to just create the field: public string Name; // all problems gone the second one is to back up your property with the private field and use in getter and setter code: private string name; // lowercase n public string Name { get { return name; } // you are noo more recursively accessing getter here set { name = value; } // same for setter } Cheers, elk.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups