0
MyFunctions MyFormulas = new MyFunctions();  

class MyFunctions

    {
        public double Rectangle[](double Length, double Width)
        {

            double[] Rectangle=new double[1];

            double AreaOfRectangle = Length * Width;
            double PerimeterOfRectangle = 2 * (Length + Width);

            Rectangle[0]=AreaOfRectangle;
            Rectangle[1]=PerimeterOfRectangle;

            return Rectangle;
        }
   }

I need something like this. returns 2 values under 1 class. is this possible?

CodeCaster
  • 147,647
  • 23
  • 218
  • 272
Vincent
  • 29
  • 1
  • 6
  • 2
    No. Return an array, a Tuple<> or a concrete class. Creating a new class isn't expensive – Panagiotis Kanavos Mar 16 '16 at 10:19
  • Yes, what is the problem with this code? (besides the length of the array) – Patrick Hofman Mar 16 '16 at 10:19
  • PS. methods return values, not classes – Panagiotis Kanavos Mar 16 '16 at 10:19
  • @PanagiotisKanavos OP already returns an array. – Patrick Hofman Mar 16 '16 at 10:19
  • @PatrickHofman not with that syntax. The return value is `double`. Perhaps the OP got a syntax error for the name `Rectangle[]` and thought C# doesn't allow array return values? – Panagiotis Kanavos Mar 16 '16 at 10:20
  • you should make a struct. – Daniel A. White Mar 16 '16 at 10:20
  • Your question is answered in the [duplicate](http://stackoverflow.com/questions/748062/how-can-i-return-multiple-values-from-a-function-in-c): use a class, struct, array or `out` parameters. If you want help getting this actual code to work, read [ask] and include an actual problem statement. – CodeCaster Mar 16 '16 at 10:23
  • @PanagiotisKanavos Why wouldn't C# allow array return values? What about the `ToArray()` extension method? – Patrick Hofman Mar 16 '16 at 10:26
  • I'd recommend to get familiar with out parameters. Example: public void CalculateRectangleData(int length, int width, out double area, out double perimeter) { // you must assign values here }. And after you invoked the method. You will have your variables filled with values. – Mitulát báti Mar 16 '16 at 10:29
  • @PatrickHofman ?? I didn't say that C# doesn't allow array return types. I said the method's declaration was wrong. Instead of `double[] Rectangle(...)` it is `double Rectangle[](...)` – Panagiotis Kanavos Mar 16 '16 at 10:30
  • Sorry, I misunderstood your comment then. @PanagiotisKanavos – Patrick Hofman Mar 16 '16 at 10:31
  • @Mitulátbáti that's a **very** bad design. It makes calling the function and handling the results harder. The code also becomes fragile as it breaks encapsulation. Any change to the out parameters will require modifications, where a class/struct or array would hide the change – Panagiotis Kanavos Mar 16 '16 at 10:32
  • i saw Tuple<> but i don't know how to do it inside my code. – Vincent Mar 16 '16 at 10:36
  • @PanagiotisKanavos Before me 3 other solutions have been discussed. I just wrote another possibility. Normally I also use a class when I have to return more values. By the way I don't like Tuples. 3-4+ values and the code becomes harder to maintain. But it's okay for 2 parameters (just like the out parameter). – Mitulát báti Mar 16 '16 at 10:46

1 Answers1

2

You have a few problems:

  • Your return type definition was wrong: append the array ([]) to the type (double), not to the method;
  • Your array has size 1. It is hard to fit two items in it. I changed it to 2.

Now the working code:

public double[] Rectangle(int Length, int Width)
{

    double[] rect = new double[2];

    double areaOfRectangle = Length * Width;
    double perimeterOfRectangle = 2 * (Length + Width);

    rect[0] = areaOfRectangle;
    rect[1] = perimeterOfRectangle;

    return rect;
}

Call it like this:

MyFunctions mf = new MyFunctions();  
double[] d = mf.Rectangle();

double areaOfRectangle = d[0];
double perimeterOfRectangle = d[1];

In other cases, you might need a Tuple instance, custom class or struct instance as return type, or an out parameter.

Something like this is an option too (With a custom class SpecialRectangle):

public class SpecialRectangle
{
    double AreaOfRectangle { get; set; }
    double PerimeterOfRectangle { get; set; }
}

public SpecialRectangle Rectangle(double Length, double Width)
{
    return new SpecialRectangle() { AreaOfRectangle = Length * Width, PerimeterOfRectangle = 2 * (Length + Width) };
}

Call it like this:

MyFunctions mf = new MyFunctions();  
SpecialRectangle s = mf.Rectangle();

double areaOfRectangle = s.AreaOfRectangle;
double perimeterOfRectangle = s.PerimeterOfRectangle;
Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
  • in my code it says bad array declarator. This code is not working when i used as a method. I need something like this. double AreaOfRec=MyFormulas.Rectangle=Rectangle[0]; PerimeterOfRec=MyFormulas.Rectangle=Rectangle[1]; – Vincent Mar 16 '16 at 10:30
  • Why not use `double[] AreaOfRec = MyFormulas.Rectangle()`? – Patrick Hofman Mar 16 '16 at 10:32