Skip to content
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

Creating HowTo for creating custom units #40

Open
MadsKirkFoged opened this issue Jan 16, 2024 · 4 comments
Open

Creating HowTo for creating custom units #40

MadsKirkFoged opened this issue Jan 16, 2024 · 4 comments

Comments

@MadsKirkFoged
Copy link
Owner

MadsKirkFoged commented Jan 16, 2024

This is a placeholder for this Task started in #19.

How should we create custom units?
Does any of the code need to change for making it easier to create custom units?

@MadsKirkFoged
Copy link
Owner Author

22122bd - Just relaxed the restrictions on some methods from Internal to public in order for users to better create their customs units

MadsKirkFoged pushed a commit that referenced this issue Jan 19, 2024
MadsKirkFoged pushed a commit that referenced this issue Jan 19, 2024
@MadsKirkFoged
Copy link
Owner Author

I have started on a Wiki post about creating custom units.
Could you try it out and see if you can get it to work?
https://github.com/MadsKirkFoged/EngineeringUnits/wiki/Costume-units

@mschmitz2
Copy link
Contributor

mschmitz2 commented Jan 22, 2024

Works for me. Obviously the 2nd method doesn't give you the getters and setter methods which I personally prefer using.

These can be added as extensions as well but it is more work. Note also that you have to use two different namespaces if you want to use the same name for the unit enum and the getter, as is the case for regular units..

Using your example, this would look like:

public static class LengthCostUnitExtensions
{
    // New unit
    public static readonly LengthCostUnit EuroPerMeter = new LengthCostUnit(CostUnit.Euro, LengthUnit.Meter);
}

public static class LengthCostExtensions
{
    // New getter
    public static double EuroPerMeter(this LengthCost lengthCost) => lengthCost.As(LengthCostUnitExtensions.EuroPerMeter);

    // New setter method
    public static LengthCost FromEuroPerMeter(double? EuroPerMeter)
    {
        if (!EuroPerMeter.HasValue)
        {
            return null;
        }

        return new LengthCost(EuroPerMeter.Value, LengthCostUnitExtensions.EuroPerMeter);
    }
}

It's not ideal since the getter is now a method instead of a property and the setter has to be called with the extensions namespace:

// ideally LengthCost lc = LengthCost.FromEuroPerMeter(30.0);
LengthCost lc = LengthCostExtensions.FromEuroPerMeter(30.0);

 // ideally Console.WriteLine(lc.EuroPerMeter);
Console.WriteLine(lc.EuroPerMeter());

@mschmitz2
Copy link
Contributor

Btw, was "Costume Units" in the wiki (both title and url) intended or a typo?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants