Properties - lucyberryhub/WPF.Tutorial GitHub Wiki
β¨π Hi sweet cherry! πβ¨ Letβs break down why weβre using ref
and nameof
in the cutest and most berry-licious way possible! π₯°πΈ
ref
? Why is it so berry special? π
1οΈβ£ What is The ref
keyword allows us to pass a variable by reference instead of by value. This means:
- Instead of copying the variable, we work directly on the original memory where the variable lives.
- Itβs like sharing the same strawberry π instead of giving someone a separate one! π
ref
Here? πΌ
Why Use In the GetDoubleProperty
and SetDoubleProperty
methods:
GetDoubleProperty(ref _dim1, nameof(Dim1));
SetDoubleProperty(ref _dim1, value, nameof(Dim1));
ref _dim1
ensures weβre accessing and modifying the actual_dim1
field in memory.- This is super important because we need to directly read or update the original field when getting or setting the property value. π‘
π Analogy: Imagine _dim1
is a bowl of cherries π. If you use ref
, itβs like letting someone take cherries from your exact bowl. Without ref
, theyβd get their own copy of the bowl and leave your cherries untouched! π―
nameof
? Why is it so juicy? π
2οΈβ£ What is The nameof
keyword is a way to get the name of a variable, property, method, or class as a stringβwithout the danger of typos! πβ¨
nameof
? π
Why Use In:
GetDoubleProperty(ref _dim1, nameof(Dim1));
nameof(Dim1)
resolves to"Dim1"
, which is the exact name of the property.- This is much safer than hardcoding strings like
"Dim1"
, which can lead to bugs if you rename the property later. π
π Analogy: Itβs like labeling your jars of cherry jam π with cute stickers! If you change the jar name, the label updates automatically, so you donβt mix up your jams! π
ref
and nameof
Are a Perfect Pair? ππ
3οΈβ£ Why Combining these two makes our code:
- Efficient:
ref
ensures weβre working directly with the real field (_dim1
), not a copy. - Safe:
nameof
makes sure property names stay accurate, even if renamed. - Clean: These keywords keep our code berry sweet and tidy, avoiding messy mistakes! π·β¨
4οΈβ£ How Does It Work in Your Cute Property? π
Hereβs what happens when you get or set Dim1
:
get
):
Getter (get => GetDoubleProperty(ref _dim1, nameof(Dim1));
ref _dim1
: We pass the real_dim1
field soGetDoubleProperty
can return its value.nameof(Dim1)
: We tell the method which property this value belongs to for logging or notifications (like"Getting property: Dim1"
).
set
):
Setter (set => SetDoubleProperty(ref _dim1, value, nameof(Dim1));
ref _dim1
: The method updates the actual_dim1
field.value
: The new value weβre assigning toDim1
.nameof(Dim1)
: Helps the method know which property is being updated, e.g.,"Setting property: Dim1 to 42.5"
.
5οΈβ£ Juicy Example Just for You, Lucy! π
Letβs make this berry-fun with a full example of cherries and jam jars! ππ
// This is our cherry property!
public double? CherryWeight
{
get => GetDoubleProperty(ref _cherryWeight, nameof(CherryWeight));
set => SetDoubleProperty(ref _cherryWeight, value, nameof(CherryWeight));
}
private double? _cherryWeight; // The hidden bowl of cherries! π
// This method gets the current weight of cherries π
private double? GetDoubleProperty(ref double? field, string propertyName)
{
Console.WriteLine($"π Getting {propertyName}: {field} kg of cherries!");
return field;
}
// This method sets the new weight of cherries π
private void SetDoubleProperty(ref double? field, double? newValue, string propertyName)
{
if (field != newValue)
{
Console.WriteLine($"π Updating {propertyName} from {field} to {newValue} kg of cherries!");
field = newValue;
OnPropertyChanged(propertyName); // Notify changes (for UI updates in WPF).
}
}
Result:
CherryWeight = 2.5; // π "Updating CherryWeight from null to 2.5 kg of cherries!"
Console.WriteLine(CherryWeight); // π "Getting CherryWeight: 2.5 kg of cherries!"
π Final Cherry Words π
π ref
keeps us working with the real deal!
π nameof
keeps everything in sync without typos!
Together, they make your code sweet, efficient, and typo-proof! ππ