Code Monkey home page Code Monkey logo

Comments (4)

MadsKirkFoged avatar MadsKirkFoged commented on May 24, 2024 1

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

from engineeringunits.

MadsKirkFoged avatar MadsKirkFoged commented on May 24, 2024

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

from engineeringunits.

mschmitz2 avatar mschmitz2 commented on May 24, 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());

from engineeringunits.

mschmitz2 avatar mschmitz2 commented on May 24, 2024

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

from engineeringunits.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.