Home > other >  Only the second variable gets multiplied (^2 for some reasons) but not the first
Only the second variable gets multiplied (^2 for some reasons) but not the first

Time:09-17

Sorry for my bad english

So I tried to make a Fraction class

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Math
{
    class Fraction
    {
        static int[] fraction = new int[2];
        public Fraction(int numer, int denom)
        {
            Console.WriteLine($"Numerator & Denominator: {numer}/{denom}\n");
            fraction[0] = numer;
            fraction[1] = denom;
        }
        public int[] GetFraction(Fraction frac)
        {
            return fraction;
        }
        public Fraction Multiply(Fraction frac1 /* Ignored */, Fraction frac2)
        {
            // The value of frac1 is 30/40
            // The value of frac2 us 100/200
            Console.WriteLine("Fraction 1 Numerator: "   GetNumer(frac1)); // Print 100
            Console.WriteLine("Fraction 1 Denominator: "   GetDenom(frac1)); // Print 200
            Console.WriteLine("Fraction 2 Numerator: "   GetNumer(frac2)); // Print 100
            Console.WriteLine("Fraction 2 Denominator: "   GetDenom(frac2)); // Print 200
            int numer = GetNumer(frac1) * GetNumer(frac2); // Return 10000 (Result of 100 * 100)
            int denom = GetDenom(frac1) * GetDenom(frac2); // Return 40000 (Result of 200 * 200)
        
            Fraction result = new Fraction(numer, denom);
            return result;
        }

        static public void Print(Fraction frac)
        {
            Console.WriteLine($"{GetNumer(frac)}/{GetDenom(frac)}");
        }   
        static public int GetNumer(Fraction frac)
        {
            return frac.GetFraction(frac)[0];
        }
        static public int GetDenom(Fraction frac)
        {
            return frac.GetFraction(frac)[1];
        }
    }
}

But when I make that instance of that class and try to call the Multiply method. Only the "frac2" parameter gets multiplied

using System;

namespace Math
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Math");
            Console.WriteLine("----");

            Fraction frac2 = new Fraction(30 /* Numerator */, 40 /* Denominator */);
            Fraction frac1 = new Fraction(100, 200);
            Fraction.Print(frac1.Multiply(frac1 /* I literally put the frac1 in there but only frac2 gets noticed */, frac2));
        }
    }
}

Please I don't know what's happening Thank you

I am using the .NET 5.0 (Visual Studio)

Pls stop stack overflow for "Mostly code"

CodePudding user response:

When you make something static, there is only ever one of them. By making the array that holds your numerator and denominator a static, you end up only having one fraction, the values being those most recently defined.

This code:

    var f1 = new Fraction(100,200);
    Fraction.Print(f1);
    var f2 = new Fraction(3,4);
    Fraction.Print(f1);

Even though the last line "prints f1" the values printed are 3/4 because your code only supports remembering one num/denom ie the most recently new'd one

Remove all the statics; it's actively hindering you. There is very seldom a reason to make a data container static in a C # program - don't do it as a routine thing

  • Related