Home > Software design >  Problem with variables while trying to make a c# library(.dll)
Problem with variables while trying to make a c# library(.dll)

Time:01-09

I want to make a library that sums 2 numbers(a and b) and then stores the value into a result variable inside a library(.dll). I tried this:

 public static void Sum(int number1, int number2, int result)
    {   
        result = number1   number2;
        
    }

but I can't make it so that in a program that uses this library, you can get the value of the result value that this function calculates and that's what I couldn't figure out for the past days. If you need any more info I will gladly provide it to you! Hope someone can help me!

CodePudding user response:

In your case you do not return any result, consider using some of the following:

    public static void Main()
    {
        var sumRes = SumResut(1, 2);
        Console.WriteLine($"SumResult = {sumRes}");

        OutSum(1, 2, out int outRes);
        Console.WriteLine($"OutSum = {outRes}");

        int refRes = 0;
        RefSum(1, 2, ref refRes);
        Console.WriteLine($"RefSum = {refRes}");
    }

    public static int SumResut(int number1, int number2)
    {
        return number1   number2;
    }

    public static void OutSum(int number1, int number2, out int result)
    {
        result = number1   number2;
    }

    public static void RefSum(int number1, int number2, ref int result)
    {
        result = number1   number2;
    }
  •  Tags:  
  • Related