In the lesson about the classes and object-oriented programming we talked about access modifiers, we will know today what fields and properties are. I will say what for use access modifiers at all and why it is better to avoid public access modifier in medium and large projects, so to the point 🙂
Let’s first start with access modifiers, why in large, medium-sized projects better will be use private modifiers than public?
Imagine that in your company where you work, your team writes witcher 4 (it’s interesting if there will be fourth part …), no matter, assume that all use public modifiers, we already know that all classes see the data with public modifier, that means changing the line of code in some part the script may cause errors in several other places.
And what if a script that still uses variables from other scripts because, for example, needs to get some value if, for example, there is a collision with an object and you need to change half of the code in it? Oooo massacre … ..
I once read the book “clean code” I really recommend to read and there it was described that in a company they were doing a very large project and there was very little time, the developers didn’t apply encapsulation, and after some time they threw the project, why ?
Well, because the more code they had written without the use of encapsulation, there were more errors and when they corrected one mistake there were a few more in its place. In short, the use of encapsulation simplifies code rebuilding later.
Of course, the reasons for writing code that causes errors in other places or is illegible or unmodified is much more eg. Not using SOLID, DRY, TDD, KISS, Refactoring, design patterns, etc. but about principles, programming techniques and design patterns will surely be created in the future a separate section 🙂
Let’s move to properties and fields, the field is simply the name of the variable declared in the class. And a property? Before I say that, let’s go back to our recipe for the cake from the previous lesson.
Suppose I want to change the recipe and change the amount of ingredients added. It looks like:
static void Main(string[] args)
{
Cake cake = new Cake();
Console.WriteLine("Enter the amount of salt");
cake.AmountOfSalt= double.Parse(Console.ReadLine());
Console.WriteLine("Enter the baking time");
cake.BakingTime = int.Parse(Console.ReadLine());
Console.WriteLine("Enter the amount of eggs");
cake.NumberOfEggs = int.Parse(Console.ReadLine());
Console.WriteLine("Enter the amount of liters of milk");
cake.HowMuchMilk = int.Parse(Console.ReadLine());
Console.WriteLine("Enter the amount of flour");
cake.HowMuchFlour = int.Parse(Console.ReadLine());
Console.WriteLine("To produce this cake it's necessery to pour in " +
cake.AmountOfSalt + "g salt, to pour in " +
cake.HowMuchMilk + "l milk, add " + cake.NumberOfEggs + "eggs, to pour in " +
cake.HowMuchFlour + "kg flour and put in the oven on "+cake.BakingTime+"min");
Console.ReadKey();
}
// our Cake class
class Cake
{
public double AmountOfSalt;
public int BakingTime;
public int NumberOfEggs;
public int HowMuchMilk;
public int HowMuchFlour;
}
What if the user types in words instead of numbers? For this there are properties, to validate the data also to modify the private field and how to use these properties? To that are so-called accessory get and set, I introduced changes in the class Cake, to understand the whole in this lesson is enough to understand this piece of code:
private double _AmountOfSalt;
public double AmountOfSalt
{
get
{
if (_AmountOfSalt < 0 || _AmountOfSalt > 50)
return 0;
else
return _AmountOfSalt;
}
set
{
if (value < 0 || value > 50)
Console.WriteLine("You gave the wrong amount of salt!");
else
_AmountOfSalt = value;
}
}
Looks complicated? Nothing more wrong, through the property AmountOfSalt property, which is public, we modify the _AmountOfSalt field, which is private and using the get and set accessor we validate the data entered by the user, this way you can modify the Cake class fields even if they are public, we have applied the rules of encapsulation, our field are safe, validated, and ready for future changes, we have made a big step forward in learning object oriented programming.
The whole Cake class looks like this:
class Cake
{
private double _AmountOfSalt;
public double AmountOfSalt
{
get
{
if (_AmountOfSalt < 0 || _AmountOfSalt > 50)
return 0;
else
return _AmountOfSalt;
}
set
{
if (value < 0 || value > 50)
Console.WriteLine("You gave the wrong amount of salt!");
else
_AmountOfSalt = value;
}
}
private int _BakingTime;
public int BakingTime
{
get
{
if (_BakingTime < 0 || _BakingTime > 100)
return 0;
else
return _BakingTime;
}
set
{
if (value < 0 || value > 100)
Console.WriteLine("You have entered the wrong baking time!");
else
_BakingTime = value;
}
}
private int _NumberOfEggs;
public int NumberOfEggs
{
get
{
if (_NumberOfEggs< 0 || _NumberOfEggs> 50)
return 0;
else
return _NumberOfEggs;
}
set
{
if (value < 0 || value > 50)
Console.WriteLine("You have given the wrong amount of eggs!");
else
_NumberOfEggs = value;
}
}
private int _HowMuchMilk;
public int HowMuchMilk
{
get
{
if (HowMuchMilk< 0 || HowMuchMilk> 50)
return 0;
else
return HowMuchMilk;
}
set
{
if (value < 0 || value > 50)
Console.WriteLine("You gave the wrong amount of milk!");
else
HowMuchMilk = value;
}
}
private static int _HowMuchFlour;
public int HowMuchFlour
{
get
{
if (_HowMuchFlour< 0 || _HowMuchFlour> 50)
return 0;
else
return _HowMuchFlour;
}
set
{
if (value < 0 || value > 50)
Console.WriteLine("You have given the wrong amount of flour!");
else
_HowMuchFlour = value;
}
}
}
Main function unchanged
This content also you can find on my blog http://devman.pl/csharplan/c-language-7-fields-properties/
If you recognise it as useful, share it with others so that others can also use it.
Leave upvote and follow and wait for next articles :) .
Well, that’s all for today, in the next lesson we will learn the so-called methods, see you!