5

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" : "";
 }
}
user229044
  • 232,980
  • 40
  • 330
  • 338
Ephraim
  • 767
  • 1
  • 8
  • 15

4 Answers4

11

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; }
Joseph
  • 25,330
  • 8
  • 76
  • 125
2

You need another setter.

private string activeFlag { 
 get { return _activeFlag; } 
}
private bool activeFlagByBool { 
 set { 
    // the value here should be the bool 
    _activeFlag = value ? " /activeFlag" : ""; 
 } 
} 
Dennis C
  • 24,511
  • 12
  • 71
  • 99
0

No, but you could use a custom TypeConverter to achieve the same thing, via TypeDescriptor. If you really wanted. Not sure it would be worth it, though, unless you desparately wanted to display it in a DataGridView, PropertyGrid, or similar.

There is also a separation of concerns issue; the purpose of the flag is to understand a boolean value - the string representation relates only to the UI. You could use a custom attribute:

[CommandLine("/activeFlag")]
public bool IsActive {get;set;}

and take it from there?

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
0

I don't believe so, have a look at this project Automatic Command Line Parsing in C# I've used this and it is a nice solution.

Lazarus
  • 41,906
  • 4
  • 43
  • 54