Forum Xamarin.Forms
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

same code used in the same class many times

Glorious18Glorious18 Member ✭✭✭

Hellooooo
Please help me in reducing the number of lines in a class page

I have the same code to be used in different if statements.

How can I use one word to replace the code each time needed?

Answers

  • JohnHardmanJohnHardman GBUniversity admin

    @Glorious18 said:
    I have the same code to be used in different if statements.
    How can I use one word to replace the code each time needed?

    You'll need to post your existing code.

  • Glorious18Glorious18 Member ✭✭✭
    edited June 25

    public void CalculateDiff(double diff, int Numerator, int Denominator) { if (diff >= 0.09 && diff < 0.1055) // diff == 0.100 { // 1 / 10 Numerator = 1; Denominator = 10; } else if (diff >= 0.1055 && diff < 0.118) // diff == 0.111 { // 1 / 9 Numerator = 1; Denominator = 9; } else if (diff >= 0.118 && diff < 0.134) // diff == 0.125 { // 1 / 8 Numerator = 1; Denominator = 8; } else if (diff >= 0.134 && diff < 0.155) // diff == 0.143 { // 1 / 7 Numerator = 1; Denominator = 7; } else if (diff >= 0.155 && diff < 0.1835) // diff == 0.167 { // 1 / 6 Numerator = 1; Denominator = 6; } else if (diff >= 0.1835 && diff < 0.211) // diff == 0.200 { // 1 / 5 Numerator = 1; Denominator = 5; } else if (diff >= 0.211 && diff < 0.254) // diff == 0.222 { // 2 / 9 Numerator = 2; Denominator = 9; } else if (diff >= 0.254 && diff < 0.293) // diff == 0.286 { // 2 / 7 Numerator = 2; Denominator = 7; } else if (diff >= 0.293 && diff < 0.3165) // diff == 0.300 { // 3 / 10 Numerator = 3; Denominator = 10; } else if (diff >= 0.3165 && diff < 0.354) // diff == 0.333 { // 1 / 3 Numerator = 1; Denominator = 3; } else if (diff >= 0.354 && diff < 0.3875) // diff == 0.375 { // 3 / 8 Numerator = 3; Denominator = 8; } else if (diff >= 0.3875 && diff < 0.4145) // diff == 0.400 { // 2 / 5 Numerator = 2; Denominator = 5; } else if (diff >= 0.4145 && diff < 0.4365) // diff == 0.429 { // 3 / 7 Numerator = 3; Denominator = 7; } else if (diff >= 0.4365 && diff < 0.472) // diff == 0.444 { // 4 / 9 Numerator = 4; Denominator = 9; } else if (diff >= 0.472 && diff < 0.528) // diff == 0.500 { // 1 / 2 Numerator = 1; Denominator = 2; } else if (diff >= 0.528 && diff < 0.5635) // diff == 0.556 { // 5 / 9 Numerator = 5; Denominator = 9; } else if (diff >= 0.5635 && diff < 0.5855) // diff == 0.571 { // 4 / 7 Numerator = 4; Denominator = 7; } else if (diff >= 0.5855 && diff < 0.6125) // diff == 0.600 { // 3 / 5 Numerator = 3; Denominator = 5; } else if (diff >= 0.6125 && diff < 0.646) // diff == 0.625 { // 5 / 8 Numerator = 5; Denominator = 8; } else if (diff >= 0.646 && diff < 0.6835) // diff == 0.667 { // 2 / 3 Numerator = 2; Denominator = 3; } else if (diff >= 0.6835 && diff < 0.707) // diff == 0.700 { // 7 / 10 Numerator = 7; Denominator = 10; } else if (diff >= 0.707 && diff < 0.732) // diff == 0.714 { // 5 / 7 Numerator = 5; Denominator = 7; } else if (diff >= 0.732 && diff < 0.764) // diff == 0.75 { // 3 / 4 Numerator = 3; Denominator = 4; } else if (diff >= 0.764 && diff < 0.789) // diff == 0.778 { // 7 / 9 Numerator = 7; Denominator = 9; } else if (diff >= 0.789 && diff < 0.8165) // diff == 0.800 { // 4 / 5 Numerator = 4; Denominator = 5; } else if (diff >= 0.8165 && diff < 0.845) // diff == 0.833 { // 5 / 6 Numerator = 5; Denominator = 6; } else if (diff >= 0.845 && diff < 0.866) // diff == 0.857 { // 6 / 7 Numerator = 6; Denominator = 7; } else if (diff >= 0.866 && diff < 0.8875) // diff == 0.875 { // 7 / 8 Numerator = 7; Denominator = 8; } else if (diff >= 0.8875 && diff <= 0.9125) // diff == 0.900 { // 9 / 10 Numerator = 9; Denominator = 10; } }

  • Glorious18Glorious18 Member ✭✭✭

    `CompuChemXamlPage9 p = new CompuChemXamlPage9();
    p.CalculateDiff(diff, Numerator, Denominator);

                        RCH = ((Math.Floor(RCH)) * Denominator) + Numerator;
                        FirstElement.Text = "C";
                        FirstElementNum.Text = Denominator.ToString();
                        SecondElement.Text = "H";
                        SecondElementNum.Text = RCH.ToString();`
    
  • Glorious18Glorious18 Member ✭✭✭

    CalculateDiff is a code that i want to use many times

    It is not read as I wrote the code

    diff is returning zero
    Numerator and Denominator are returning zero too

  • JohnHardmanJohnHardman GBUniversity admin

    @Glorious18

    First things first. This is a forum for Xamarin.Forms questions. Your question is a C# question, with nothing specific to Xamarin.Forms. It should therefore be on StackOverflow, not here.

    As it happens, pretty much the same question has been asked on StackOverflow before. Take a look at https://stackoverflow.com/questions/20147879/switch-case-can-i-use-a-range-instead-of-a-one-number where various possible answers are provided for switching based on numeric ranges. Using that in combination with named or unnamed tuples will provide what you need - see https://docs.microsoft.com/en-us/dotnet/csharp/tuples . Have CalculateDiff return the tuple rather than void (you currently have a bug where you are updating Numerator and Denominator, which are both local variables).

    Obviously, be aware of the usual issues around comparing doubles. And add an else or default case.

Sign In or Register to comment.