Valerie Meunier

Valerie Meunier

  • 962
  • 693
  • 72.3k

More general output

Dec 24 2021 11:42 AM

Hi,

The output of this is: (assume i enter 5): 5 x 3 = 15

But in the Console.WriteLine, i have to put '3' which is the value used by the method Prod.

My question is: by what could i replace '3' to be general? I tried with myObj.b but the value is still 5. I tried with myObj3.b but this gives an error (unreachable).

Thanks

using System;
class Hoofd
        {
            public int b = 2;
            static void Main()
            {
                int g1 = 0;
                Console.Write("Voer een geheel getal: ");
                g1 = Convert.ToInt32(Console.ReadLine());

                Hoofd myObj = new Hoofd();
                myObj.b = 5;
                Prod myObj2 = new Prod();
                Console.WriteLine("{0} x {1} = {2}", g1, 3, myObj2.product(g1, myObj.b));
            }
        }
        class Prod
        {
            Hoofd myObj3 = new Hoofd();//of in de method

            public int product(int x, int y)
            //public => bereikbaar vanuit Main
            {
                myObj3.b = 3;
                return x * myObj3.b;
            }
        }
 


Answers (4)