-
Notifications
You must be signed in to change notification settings - Fork 387
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Best practices for working with dynamic units? #587
Comments
I would generally populate a combo box with the pressure units. I use a wrapper like so to store a string/value: internal class ComboBoxItemWrapper<T>
{
private T item;
private readonly Func<ComboBoxItemWrapper<T>, string> toStringMethod;
public ComboBoxItemWrapper( T item, string text )
{
this.item = item;
this.toStringMethod = cbiw => text;
}
public ComboBoxItemWrapper( T item, Func<ComboBoxItemWrapper<T>, string> toStringMethod )
{
this.item = item;
this.toStringMethod = toStringMethod;
}
public override bool Equals( object obj )
{
if( obj is T )
return Item.Equals( obj );
else
return base.Equals( obj );
}
public T Item
{
get { return item; }
}
public override string ToString()
{
return toStringMethod( this );
}
} And populate a combo box like so: foreach( PressureUnit unit in Enum.GetValues( typeof( PressureUnit ) ) )
{
if( unit == PressureUnit.Undefined )
continue;
var comboBoxItem = new ComboBoxItemWrapper<PressureUnit>( unit, UnitAbbreviationsCache.Default.GetDefaultAbbreviation( unit ) );
pressureChoiceComboBox.Items.Add( comboBoxItem );
} Then you can switch like so: var selectedPressureUnit = ( (ComboBoxItemWrapper<PressureUnit>)pressureChoiceComboBox.SelectedValue ).Item;
var pressure = Pressure.From( Control.Value, selectedPressureUnit ); |
I think you should get far by using this:
|
Thanks you two for your feedback! If I make a new pressure unit: And then change its unit: Displaying it value: The output is displayed as the Also if I do: It prints it name: Is there another way to display its units rather then its name? |
_pressure.As(NewUnit); does not change the value of _pressure, it returns the converted pressure. var converted = _pressure.As(NewUnit);
Debug.Print(converted.Value); As for the unit, it's just an enum. In that case you're doing a .ToString on an enum. To get the abbreviation, do the following: UnitAbbreviationsCache.Default.GetDefaultAbbreviation(NewUnit) |
To be clear, converted here is
To change the unit, use Is it possible you simply want to do this? string psiText = _pressure.ToUnit(PressureUnit.PoundForcePerSquareInch).ToString(); // "20 psi" |
I just want it without the Right now I can get the value + unit: Or I can get the value: Or I can get the name: Im just missing the unit in a string: @angularsen Looks great that you're already looking into improve working dynamically with units! |
Ah, then what Tristan proposed should work for you.
|
I posted from the the wrong account :-) |
Ok, I'm closing this issue. Just reopen if there is anything else. |
I'm working on a program where the customer wants to be able to change the 'default units'.
ex
In a Settings-tab:
Pressure (choose)
Bar or Pascal.
If you select 'Bar' then all input and output are displayed in Bar.
Right now I have hard coded it like:
What is Best practices for making dynamic units?
The text was updated successfully, but these errors were encountered: