Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialStijn Van Wijmeersch
5,663 PointsReturn a boolean when using more complex setters?
Hi,
I have some experience with OOP & I often return a boolean in more complex setter methods. This makes it easy to check if my values are set or not. Is it interesting to use this in C# as well?
2 Answers
Steven Parker
231,275 PointsWhy would you need a return code?
Normally, a properly written setter can never fail, so no return is necessary.
Under what conditions would you expect a setter might not work?
Stijn Van Wijmeersch
5,663 PointsUntil now I'm used to programming for the web. We use lots of libraries & depend client sided technologie (outdated browsers for example).
While testing we find out that code is not always executed the same way, depending on the speed of the internet connection, the power of the computer, the browser the user is surfing with & how much load there currently is on our servers.
I like confirmation when something was set successfully. That way we can be confident the user can continue with the application, wizard, ... without getting an error, losing data, ... On the other hand, when something went wrong, we can inform our user/visitor.
Steven Parker
231,275 PointsThese setter methods only affect internal class variables so they have no failure scenarios. If control returns to the caller at all, you can be certain nothing "went wrong".
Steven Parker
231,275 PointsSteven Parker
231,275 PointsWith these kinds of setter methods, you certainly could return a result code if it was really needed.
But in following videos, the concept of class property setters will be introduced, which can not return anything. These setter methods are intended to introduce the concept of a "setter" in a compatible way and so they are intentionally void.