tags:

views:

62

answers:

4

hey,

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" : "";
 }
}

Ciao Ephraim

+3  A: 

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
I would expect an "Is..." method to return true/false...
Marc Gravell
@Marc Yeah you're right.. it's too early =P I'll edit to reflect.
Joseph
A: 

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 Cheung
A: 

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
A: 

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