Matthew Tang Matthew Tang - 1 month ago 9
C# Question

C# compile error for making a method. works if inside main()

I had a code working for reducing triplet ratios in Main(), until I started to make a separate method to doing so, it won't compile. Here is my code so far.

What I wanted to do was to call SimplifyRatio(a,b,c); and it will cough out a reduced form of the ratios.

Say,

SimplifyRatio(2,4,8);


I should get

1:2:4


except, the method requires me to have a return value but I can't return strings as well. I tried

return ratio1_reduce+":"+ratio2_reduce+":"+ratio3_reduce;


but obviously I can't return both int and string. I read over other posts saying to create another method entirely for getSimplifiedRatio(); but I can't even get this method to work.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;

namespace Math {

public class Ratio {

public void SimplifyRatio(int ratio1, int ratio2, int ratio3) {
this.ratio1 = ratio1;
this.ratio2 = ratio2;
this.ratio3 = ratio3;
int divisor;

if (ratio1>ratio2 && ratio1>ratio3) {
divisor = ratio1;
}
else if (ratio2>ratio1 && ratio2>ratio3) {
divisor = ratio2;
}
else divisor = ratio3;


int ratio1_reduce = ratio1;
int ratio2_reduce = ratio2;
int ratio3_reduce = ratio3;
while (divisor!=0) {

if(ratio1_reduce%divisor==0 && ratio2_reduce%divisor==0 && ratio3_reduce%divisor==0){
int ratio1_reduce_temp = ratio1_reduce/divisor;
int ratio2_reduce_temp = ratio2_reduce/divisor;
int ratio3_reduce_temp = ratio3_reduce/divisor;

ratio1_reduce = ratio1_reduce_temp;
ratio2_reduce = ratio2_reduce_temp;
ratio3_reduce = ratio3_reduce_temp;
divisor--;
}
else divisor--;

}

}


public int getRatio1() {
return ratio1_reduce;
}

public static void Main(string[] args) {
SimplifyRatio(8,10,20);

}

}

}

Answer

You're using this statement as if you're approaching this problem using Object Oriented Programming. If you want to go that way, make a new class, simply:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;

namespace Math {
    public class SimplifyRatio{
        public int ratio1_reduce;
        public int ratio2_reduce;
        public int ratio3_reduce;

        public SimplifyRatio(int ratio1, int ratio2, int ratio3) {
            int divisor;

            if (ratio1>ratio2 && ratio1>ratio3) {
                divisor = ratio1;
             }
            else if (ratio2>ratio1 && ratio2>ratio3) {
                divisor = ratio2;
            }
            else divisor = ratio3;


            this.ratio1_reduce = ratio1;
            this.ratio2_reduce = ratio2;
            this.ratio3_reduce = ratio3;
            while (divisor!=0) {

                if(ratio1_reduce%divisor==0 && ratio2_reduce%divisor==0 && ratio3_reduce%divisor==0){
                    int ratio1_reduce_temp = ratio1_reduce/divisor;
                    int ratio2_reduce_temp = ratio2_reduce/divisor;
                    int ratio3_reduce_temp = ratio3_reduce/divisor;

                    ratio1_reduce = ratio1_reduce_temp;
                    ratio2_reduce = ratio2_reduce_temp;
                    ratio3_reduce = ratio3_reduce_temp;
                    divisor--;
                }
                else divisor--;

            }
        }
    }

    public class Ratio{
        public static void Main(String[] args){
            SimplifyRatio sr = new SimplifyRatio(2,4,8);
            Console.WriteLine(sr.ratio1_reduce+":"+sr.ratio2_reduce+":"+sr.ratio3_reduce);
        }
    }
}

This is not good OOP code by the way. It simply just does what you want to do.

As you wish:

public static int[] SimplifyRatio(int ratio1, int ratio2, int ratio3) {
    int divisor;

    if (ratio1>ratio2 && ratio1>ratio3) {
        divisor = ratio1;
     }
    else if (ratio2>ratio1 && ratio2>ratio3) {
        divisor = ratio2;
    }
    else divisor = ratio3;


    int ratio1_reduce = ratio1;
    int ratio2_reduce = ratio2;
    int ratio3_reduce = ratio3;
    while (divisor!=0) {

        if(ratio1_reduce%divisor==0 && ratio2_reduce%divisor==0 && ratio3_reduce%divisor==0){
            int ratio1_reduce_temp = ratio1_reduce/divisor;
            int ratio2_reduce_temp = ratio2_reduce/divisor;
            int ratio3_reduce_temp = ratio3_reduce/divisor;

            ratio1_reduce = ratio1_reduce_temp;
            ratio2_reduce = ratio2_reduce_temp;
            ratio3_reduce = ratio3_reduce_temp;
            divisor--;
        }
        else divisor--;

    }

    return new int[]{ratio1_reduce, ratio2_reduce, ratio3_reduce};

}

Tested: http://rextester.com/GAPTZ65968

EDIT: Updated code

Comments