0

From my main code I'm calling a method of a class meth2 which calls another method from another class meth1. It is important for me to respect this structure. These methods make an assignment of values that are previously defined in the classes but I am not able to get a proper result in my command window (just a 0 instead of a 132). I assume I'm missing something.

Does anybody has an idea? Here's the code:

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

namespace workingWithClasses
{
    class meth1
    {
        public int op1;

        public int multiply(int mult)
        {
            return op1 = 44 * mult;
        }
    }

    class meth2
    {
        public int CallFuncsClass(int multiplicator)
        {
            return m1.multiply(int multiplicator);
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            meth1 m1 = new meth1();
            meth2 m2 = new meth2();

            m2.CallFuncsClass(3);
            int result_m1 = m1.op1;         

            Console.WriteLine(opm1);
            Console.ReadKey();
        }
    }
}

Thanks a lot!

pravprab
  • 2,301
  • 3
  • 26
  • 43
ironzionlion
  • 832
  • 1
  • 7
  • 28

5 Answers5

1

This code won't compile, right? This line return m1.multiply(int multiplicator); is out of the place. You need to define what is m1. I guess you are looking for dependency injection. You can do this via constructor, so

class meth2
{
    private meth1 _m1;

    meth2(meth1 m1)
    {
        if(m1 == null) throw new ... // check input params

        _m1 = m1;
    }

    public int CallFuncsClass(int multiplicator)
    {
        return _m1.multiply(int multiplicator);
    }
}

The usage will be

meth1 m1 = new meth1();
meth2 m2 = new meth2(m1);

m2.CallFuncsClass(3);

Bonus points

  • Name your classes correctly, instead of meth1 it should be something like Calculator
  • Don't use public fields: public int op1;. It should be private
  • Usually you would want your classes to be public, by default the class is internal. In this way you can use it outside a single library
  • Check parameters for correct value, throw exception if something is incorrect
oleksii
  • 35,458
  • 16
  • 93
  • 163
1
namespace workingWithClasses
{
    public class meth1
    {
        public int op1;

        public int multiply(int mult)
        {
            return op1 = 44 * mult;
        }
    }

    public class meth2
    {
        meth1 m1 = new meth1();
        public int CallFuncsClass(int multiplicator)
        {
            return m1.multiply( multiplicator);
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
           meth2 m2 = new meth2();

           int result_m2 = m2.CallFuncsClass(3);
           Console.WriteLine(result_m2);

           Console.ReadKey();
        }
    }
}
pravprab
  • 2,301
  • 3
  • 26
  • 43
0

Make multiply static on class meth1:

public static int multiply(int mult)

Also don't use 'op1', just return the result of the operation.

Call like this:

return meth1.multiply(int multiplicator);
0

Here is the solution

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

namespace workingWithClasses
{
    class meth1
    {
        public int op1;

        public int multiply(int mult)
        {
            return op1 = 44 * mult;
        }
    }

    class meth2
    {
        public int CallFuncsClass(int multiplicator)
        {
            meth1 m1=new meth1();
            return m1.multiply(multiplicator);
        }
    }

    class Program
    {
        static void Main(string[] args)
        {

            meth2 m2 = new meth2();

            int result_m1=m2.CallFuncsClass(3);

            Console.WriteLine(result_m1);
            Console.ReadKey();
        }
    }
}
Chirag
  • 324
  • 2
  • 4
  • 27
  • Thanks for your answer, but I am still getting 0 as result. The variable assigned in `meth1` is not recognized in the main file – ironzionlion Jan 30 '14 at 10:49
  • @user3252535 check my updated answer. updated code **int result_m1=m2.CallFuncsClass(3);** – Chirag Jan 30 '14 at 10:56
-1

If you want to call methods from meth1 from within meth2 like that (without creating an object of type meth1) you need to make multiply() static.

This will allow it to be used in the manner you're using:

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

namespace workingWithClasses
{
    class meth1
    {
        //note statics
        static int op1;

        static int multiply(int mult)
        {
            return op1 = 44 * mult;
        }
    }

    class meth2
    {
        public int CallFuncsClass(int multiplicator)
        {
            //access to multiply() is valid here
            return meth1.multiply(multiplicator);
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            meth1 m1 = new meth1();
            meth2 m2 = new meth2();

            m2.CallFuncsClass(3);
            int result_m1 = m1.op1;         

            Console.WriteLine(opm1);
            Console.ReadKey();
        }
    }
}
Chris L
  • 2,262
  • 1
  • 18
  • 33
  • basically this line is wrong `return meth1.multiply(int multiplicator);`. You must have to write like `return meth1.multiply(multiplicator);` – Chirag Jan 31 '14 at 12:56