❔ How to set relative properties ?

SelectedCallingCode.CallingCode: +31 PhoneNumber: 123456 FullPhoneNumber: +31 123456 Are these syntaxsyntaxes correct on the FullPhoneNumber ?
16 Replies
Pobiega
Pobiega2y ago
not really, no. You are declaring it a normal getter and setter, but its actually a "calculated" property that only uses other props to get its value the setter doesnt use value, as you can see You'll need to raise PropertyChanged events for FullPhoneNumber in the other setters but have it declared simply as public string FullPhoneNumber => $"{SelectedCallingCode?.CallingCode} {PhoneNumber}";
TotechsStrypper
Can you make this with a + in it ? "+"
Pobiega
Pobiega2y ago
ofc just add the +
TotechsStrypper
okay oh shoot I forgot these are really from my boss These props suppose to have inotifyprop change
Pobiega
Pobiega2y ago
Im assuming thats what SetProperty does its a very common way to implement it
TotechsStrypper
Do you think SetProp related to that yeah I think too
Pobiega
Pobiega2y ago
but you need to raise two events here because when PhoneNumber changes, FullPhoneNumber also changes same for calling code I know the MVVM toolkit source generators support this, but in this case you might need to manually implement it
TotechsStrypper
the dude ban me from using it because he want these crap to able to work with blazor
Pobiega
Pobiega2y ago
¯\_(ツ)_/¯
MODiX
MODiX2y ago
Pobiega#2671
REPL Result: Success
var phoneNumber = "123";
var callingCode = "31";

Console.WriteLine($"+{callingCode} {phoneNumber}");
var phoneNumber = "123";
var callingCode = "31";

Console.WriteLine($"+{callingCode} {phoneNumber}");
Console Output
+31 123
+31 123
Compile: 515.197ms | Execution: 70.452ms | React with ❌ to remove this embed.
TotechsStrypper
I tried this method but the prop require raise change for UI to work
Pobiega
Pobiega2y ago
Which is why I told you to raise events for that prop in the other props setters since changing either of them will also change that value
TotechsStrypper
Ah I get it
Pobiega
Pobiega2y ago
public string CallingCode
{
get => _callingCode;
set
{
SetField(ref _callingCode, value);
OnPropertyChanged(nameof(FullPhoneNumber));
}
}

public string PhoneNumber
{
get => _phoneNumber;
set
{
SetField(ref _phoneNumber, value);
OnPropertyChanged(nameof(FullPhoneNumber));
}
}

public string FullPhoneNumber => $"+{CallingCode} {PhoneNumber}";
public string CallingCode
{
get => _callingCode;
set
{
SetField(ref _callingCode, value);
OnPropertyChanged(nameof(FullPhoneNumber));
}
}

public string PhoneNumber
{
get => _phoneNumber;
set
{
SetField(ref _phoneNumber, value);
OnPropertyChanged(nameof(FullPhoneNumber));
}
}

public string FullPhoneNumber => $"+{CallingCode} {PhoneNumber}";
TotechsStrypper
Thanks mate
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.