C# using get set. Why is this failing?
-
Seriously: Are there really people out there NOT using a debugger when developing code? To me that is like climbing Mt.Everest blindfolded, all on your own. There may be extreme exceptions where no traditional debugger is available or can't be used, e.g. in a certain class of embedded systems where the code is in flash memory and cannot be modified on a byte or word basis, so you can't poke debug interrupts. Or the processor is so primitive that it doesn't provide interrupts, or doesn't give sufficient external access to the address bus to run a probe on the outside. But those are very special cases. And I have never seen a system where you cannot do "printf style" debugging by setting output signals - even 8-bit embedded processors offer that (otherwise, how would they control what they are supposed to control?) Those special cases are lightyears away from desktop development in C#. If you run a company, you most certainly can affort the investment in, say, Visual Studio. For hobby programming, there is a free version providing so near-100% functionality that you probably won't notice the difference. (There are some differences, but they are rarely relevant for hobby programming.) Even printf debugging can work well in some cases (sometimes it is usesful as a companion to the interactive debugger, e.g. to traverse data structures and print selected values if this requires more processing than a simple watch expression). But "debugging by cranial massage", as one University lecturer phrased it. is not sufficient.
-
I normally have an error message popup that tells me e exactly what is wrong (in most cases it's a spelling mistake) but this time this did not happen. I have stepped through programs to see how they work. I could try doing that but I suspect all that would happen is the same message would pop up which does not tell me exactly what is wrong. However a popup box with same message is better than nothing. I may need to research this message on the internet Brian
There is no message you could possibly get that will tell you "exactly what is wrong" when the logic in your app is what is wrong. The error message you get can even be misleading. Using the debugger to step through the code is THE most powerful tool you can use to find problems, not error messages. Error messages are a symptom of the problem, not the solution to it.
Asking questions is a skill CodeProject Forum Guidelines Google: C# How to debug code Seriously, go read these articles.
Dave Kreskowiak -
_Gold is a member variable. Gold is the property that exposes the variable. Basically, if you attempt to get a property and return the property name instead of a variable, you're going to repeatedly call the same getter. This is what causes the StackOverflowException.
This space for rent
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,
-
Seriously: Are there really people out there NOT using a debugger when developing code? To me that is like climbing Mt.Everest blindfolded, all on your own. There may be extreme exceptions where no traditional debugger is available or can't be used, e.g. in a certain class of embedded systems where the code is in flash memory and cannot be modified on a byte or word basis, so you can't poke debug interrupts. Or the processor is so primitive that it doesn't provide interrupts, or doesn't give sufficient external access to the address bus to run a probe on the outside. But those are very special cases. And I have never seen a system where you cannot do "printf style" debugging by setting output signals - even 8-bit embedded processors offer that (otherwise, how would they control what they are supposed to control?) Those special cases are lightyears away from desktop development in C#. If you run a company, you most certainly can affort the investment in, say, Visual Studio. For hobby programming, there is a free version providing so near-100% functionality that you probably won't notice the difference. (There are some differences, but they are rarely relevant for hobby programming.) Even printf debugging can work well in some cases (sometimes it is usesful as a companion to the interactive debugger, e.g. to traverse data structures and print selected values if this requires more processing than a simple watch expression). But "debugging by cranial massage", as one University lecturer phrased it. is not sufficient.
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
-
There is no message you could possibly get that will tell you "exactly what is wrong" when the logic in your app is what is wrong. The error message you get can even be misleading. Using the debugger to step through the code is THE most powerful tool you can use to find problems, not error messages. Error messages are a symptom of the problem, not the solution to it.
Asking questions is a skill CodeProject Forum Guidelines Google: C# How to debug code Seriously, go read these articles.
Dave KreskowiakI created an error to test out the debugger. I then pressed F5 to turn on the debugger, but when doing that the program compiles and tells me the error before I can step thought it. Brian
-
[Using Properties - C# Programming Guide | Microsoft Docs](https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/using-properties)
The Master said, 'Am I indeed possessed of knowledge? I am not knowing. But if a mean person, who appears quite empty-like, ask anything of me, I set it forth from one end to the other, and exhaust it.' ― Confucian Analects
Gerry, I'm grateful for any knowledge I receive. Brian
-
I created an error to test out the debugger. I then pressed F5 to turn on the debugger, but when doing that the program compiles and tells me the error before I can step thought it. Brian
Show the code. Hitting F5 doesn't "turn on the debugger". It launches the result of compiling whatever is tagged as the "Startup project", and its dependencies, then the debugger is "attached" to the launched process. There are two sets of error messages, compile-time and run-time. All of the messages that tell you "what's wrong and how to fix it" are at compile-time, before you even have an .EXE to run. These are syntax problems with your code. The other messages show up at run-time and tell you how your code failed to execute somewhere. These are related to very specific problems with some action your code is executing, such as "Access Denied" or "File Not Found". These message never show up at compile-time and will only give you a hint of what to look for when stepping through your code and inspecting variables. These are problems with the logic in your code. Quite often you can write syntactically correct code and it will compile, but when it comes to running it, you can have all kinds of problems.
Asking questions is a skill CodeProject Forum Guidelines Google: C# How to debug code Seriously, go read these articles.
Dave Kreskowiak -
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
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 -
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 KreskowiakI 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
-
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,
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
-
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
Thanks Pete. That was easier to fix than I thought it might be. Brian
-
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
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
-
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
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
-
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
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
-
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
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
-
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
@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
-
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