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. C# using get set. Why is this failing?

C# using get set. Why is this failing?

Scheduled Pinned Locked Moved C#
csharphelpquestion
33 Posts 9 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.
  • B Brian_TheLion

    But isn't the program debugging when it compiles. I normally get an error message if the compiler can't compile the code. Maybe your talking about some other debugging. Brian

    D Offline
    D Offline
    Dave Kreskowiak
    wrote on last edited by
    #24

    Brian_TheLion wrote:

    But isn't the program debugging when it compiles.

    No. Errors at compile-time are just syntactic problems with your code. The compiler cannot compile the code into an executable when these show up. They have nothing at all to do with the logic of your code. Debugging comes into play when you actually start running the code.

    Asking questions is a skill CodeProject Forum Guidelines Google: C# How to debug code Seriously, go read these articles.
    Dave Kreskowiak

    B 1 Reply Last reply
    0
    • D Dave Kreskowiak

      Brian_TheLion wrote:

      But isn't the program debugging when it compiles.

      No. Errors at compile-time are just syntactic problems with your code. The compiler cannot compile the code into an executable when these show up. They have nothing at all to do with the logic of your code. Debugging comes into play when you actually start running the code.

      Asking questions is a skill CodeProject Forum Guidelines Google: C# How to debug code Seriously, go read these articles.
      Dave Kreskowiak

      B Offline
      B Offline
      Brian_TheLion
      wrote on last edited by
      #25

      I think I understand now what you are meaning Dave. It's when the program does run but gives you the wrong results is the other type of debugging. Brian

      L 1 Reply Last reply
      0
      • B Brian_TheLion

        Hi Griff. Your change worked when there was a test for the value of 20 but when I changed the test value to 30 then 0 was displayed on the form for the amount of gold, then when I clicked on the Test button the gold value changed to 35. What I'm aiming at on this test is for 20 to first to appear on the form for the Gold value after the program is run, then when I press the Test button the gold value should change to 30 and in the test have 5 added to the 30 giving a gold value of 35. Here is my code On the main program I have SuperAdventure.cs

        public partial class SuperAdventure : Form { private Player _player; public SuperAdventure() { InitializeComponent(); Location location = new Location(1, "Home", "This is your house."); _player = new Player(10, 10, 20, 0, 1); Update(); } // TEST Button on Form private void btnTest_Click(object sender, EventArgs e) { _player.Gold = 30; Update(); } public void Update() { lblHitPoints.Text = _player.CurrentHitPoints.ToString(); lblGold.Text = _player.Gold.ToString(); lblExperience.Text = _player.ExperiencePoints.ToString(); lblLevel.Text = _player.Level.ToString(); } } } Player.cs code namespace Engine { public class Player { // This is needed for _player = new Player(10, 10, 20, 0, 1,); public int CurrentHitPoints { get; set; } public int MaximumHitPoints { get; set; } private int _Gold = 0; public int Gold { get { return _Gold; } set { if (value == 30) { value += 5; _Gold = value; } } } public int ExperiencePoints { get; set; } public int Level { get; set; } //In order to use lists – we need one variable or property to hold a collection of objects that are the same class public List Inventory { get; set; } public List Quests { get; set; } // This is needed for lblHitPoints.Text = _player.CurrentHitPoints.ToString() etc; public Player(int currentHitPoints, int maximumHitPoints, int gold,

        P Offline
        P Offline
        Pete OHanlon
        wrote on last edited by
        #26

        The problem you have is that you don't assign anything to _Gold unless value is 30. This is because you do _Gold = value inside the if statement. Move this line outside the if block in your setter and it should start working.

        This space for rent

        B 1 Reply Last reply
        0
        • P Pete OHanlon

          The problem you have is that you don't assign anything to _Gold unless value is 30. This is because you do _Gold = value inside the if statement. Move this line outside the if block in your setter and it should start working.

          This space for rent

          B Offline
          B Offline
          Brian_TheLion
          wrote on last edited by
          #27

          Thanks Pete. That was easier to fix than I thought it might be. Brian

          1 Reply Last reply
          0
          • B Brian_TheLion

            Tbanks Griff. It looks like I've created a continuous loop. I'll try what you have suggested. I noticed that you used _Gold instead of Gold. Brian

            B Offline
            B Offline
            Brian_TheLion
            wrote on last edited by
            #28

            Thanks everyone who has helped me with this code problem. I now have a way of sending a value to a class, have the class change the value (if the test passes) and have the class return the result (which is displayed on the form. Brian

            B 1 Reply Last reply
            0
            • B Brian_TheLion

              Thanks everyone who has helped me with this code problem. I now have a way of sending a value to a class, have the class change the value (if the test passes) and have the class return the result (which is displayed on the form. Brian

              B Offline
              B Offline
              BillWoodruff
              wrote on last edited by
              #29

              Do keep in mind that MS Guidelines suggest using Properties only as gateways to internal fields [^]

              Quote:

              In general, methods represent actions and properties represent data. Properties are meant to be used like fields, meaning that properties should not be computationally complex or produce side effects. When it does not violate the following guidelines, consider using a property, rather than a method, because less experienced developers find properties easier to use.

              not to produce 'side-effects" by way of internal computation in the setter and/or getter ... and advocates using methods to replace any internal computation. While I feel "relaxed" about some computation in a Property setter/getter, there are folks who have strong opinions this is bad practice. Another argument against computation in Properties is that end-users of your code may not be aware of the "cost" of access. Of course, you can implement INotifyPropertyChanged as a "trigger" for some external action.

              «Where is the Life we have lost in living? Where is the wisdom we have lost in knowledge? Where is the knowledge we have lost in information?» T. S. Elliot

              B 1 Reply Last reply
              0
              • B BillWoodruff

                Do keep in mind that MS Guidelines suggest using Properties only as gateways to internal fields [^]

                Quote:

                In general, methods represent actions and properties represent data. Properties are meant to be used like fields, meaning that properties should not be computationally complex or produce side effects. When it does not violate the following guidelines, consider using a property, rather than a method, because less experienced developers find properties easier to use.

                not to produce 'side-effects" by way of internal computation in the setter and/or getter ... and advocates using methods to replace any internal computation. While I feel "relaxed" about some computation in a Property setter/getter, there are folks who have strong opinions this is bad practice. Another argument against computation in Properties is that end-users of your code may not be aware of the "cost" of access. Of course, you can implement INotifyPropertyChanged as a "trigger" for some external action.

                «Where is the Life we have lost in living? Where is the wisdom we have lost in knowledge? Where is the knowledge we have lost in information?» T. S. Elliot

                B Offline
                B Offline
                Brian_TheLion
                wrote on last edited by
                #30

                Hi Bill. You wrote that INotifyPropertyChanged can be used as a "trigger" for some external action. Can you use this to auto add text to a rich text window on a form when you change the string variable in a class that the rich text window is connected to? Brian

                B 2 Replies Last reply
                0
                • B Brian_TheLion

                  Hi Bill. You wrote that INotifyPropertyChanged can be used as a "trigger" for some external action. Can you use this to auto add text to a rich text window on a form when you change the string variable in a class that the rich text window is connected to? Brian

                  B Offline
                  B Offline
                  BillWoodruff
                  wrote on last edited by
                  #31

                  Brian_TheLion wrote:

                  Can you use this to auto add text to a rich text window on a form when you ...

                  Theoretically, given the right structure, and valid references to run-time objects, the handler(s) that subscribe to the event can do anything any other method can do. Take a look at the examples here: [^]

                  «Where is the Life we have lost in living? Where is the wisdom we have lost in knowledge? Where is the knowledge we have lost in information?» T. S. Elliot

                  1 Reply Last reply
                  0
                  • B Brian_TheLion

                    Hi Bill. You wrote that INotifyPropertyChanged can be used as a "trigger" for some external action. Can you use this to auto add text to a rich text window on a form when you change the string variable in a class that the rich text window is connected to? Brian

                    B Offline
                    B Offline
                    BillWoodruff
                    wrote on last edited by
                    #32

                    @Brian_TheLion good article on INotifyPropertyChanged: [^]

                    «Where is the Life we have lost in living? Where is the wisdom we have lost in knowledge? Where is the knowledge we have lost in information?» T. S. Elliot

                    1 Reply Last reply
                    0
                    • B Brian_TheLion

                      I think I understand now what you are meaning Dave. It's when the program does run but gives you the wrong results is the other type of debugging. Brian

                      L Offline
                      L Offline
                      lmoelleb
                      wrote on last edited by
                      #33

                      Personally I would never call fixing compile time errors "debugging".

                      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