Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert type in setter method, possible? When yes how?

guess I wanted to gernerate a commandline with flags and so on. Flags are of type bool but the commandline is a string like " /activeFlag". Is there a way to program a setter in C# which takes a bool but the getter returns a string?

like

private string activeFlag {
 get { return activeFlag; }
 set {
    // the value here should be the bool
    activeFlag = value ? " /activeFlag" : "";
 }
}
like image 723
Ephraim Avatar asked Dec 18 '22 05:12

Ephraim


2 Answers

There no way to have a property with different data types for its setter and getter.

What you can do is something like this:

private bool IsActiveFlagSet { get { return ActiveFlag == " /activeFlag"; } }
private string ActiveFlag { get; set; }
like image 150
Joseph Avatar answered Dec 19 '22 17:12

Joseph


You need another setter.

private string activeFlag { 
 get { return _activeFlag; } 
}
private bool activeFlagByBool { 
 set { 
    // the value here should be the bool 
    _activeFlag = value ? " /activeFlag" : ""; 
 } 
} 
like image 44
Dennis C Avatar answered Dec 19 '22 19:12

Dennis C