2

How can return a bool from this double array method?

public static double[] GetUIPosition(string name)
    {
        if (FastUICheck.FastUICheckVisible(name, 0) == true)
        {
            UIControl control = new UIControl(Engine.Current.Memory, Engine.Current.ObjectManager.x984_UI.x0000_Controls.x10_Map[name].Value.Address);
            double[] point = new double[4];
            point[0] = control.x4D8_UIRect.Left;
            point[1] = control.x4D8_UIRect.Top;
            point[2] = control.x4D8_UIRect.Right;
            point[3] = control.x4D8_UIRect.Bottom;

            return point;

        }
        else
        {
            return false;
        }
   }

So basically i am checking if a control exists in memory and is visible and if yes then i want to get it's rect. So if yes i return an array with the 4 points else i want to return false.

Is there an easy way to do this?

George H.
  • 87
  • 8

1 Answers1

6

No, bool is not castable to dobule[].

However, you can just return null and check for that as your "false" value.

You could also take the TryParse approach and return a bool, with the double[] as an out parameter. The signature would be:

public static bool GetUIPosition(string name, out double[] position)

Your code returning null:

public static double[] GetUIPosition(string name)
{
    if (FastUICheck.FastUICheckVisible(name, 0) == true)
    {
       UIControl control = new UIControl(Engine.Current.Memory, Engine.Current.ObjectManager.x984_UI.x0000_Controls.x10_Map[name].Value.Address);
       double[] point = new double[4];
       point[0] = control.x4D8_UIRect.Left;
       point[1] = control.x4D8_UIRect.Top;
       point[2] = control.x4D8_UIRect.Right;
       point[3] = control.x4D8_UIRect.Bottom;

       return point;  
   }
   else
   {
      return null;
   }
}

A similar question with helpful answers: How can I return multiple values from a function in C#?

Community
  • 1
  • 1
BradleyDotNET
  • 60,462
  • 10
  • 96
  • 117
  • 1
    ... or to save from further comparison to `null` hell one could throw an exception – zerkms Jun 19 '14 at 00:32
  • 2
    @zerkms, true, but I would only throw an exception if you would expect that the condition would *never* return false under normal operating conditions. – BradleyDotNET Jun 19 '14 at 00:33
  • 1
    it depends. If you're invoking `GetUIPosition` for something that doesn't exist it is no different from getting file attributes for the file that doesn't exist as well (and that does throw an IO exception) – zerkms Jun 19 '14 at 00:35
  • or you could do it like the `TryParse` pattern. – Daniel A. White Jun 19 '14 at 00:35
  • @DanielA.White, I had that in my answer... perhaps it needs to be more obvious – BradleyDotNET Jun 19 '14 at 00:36
  • @zerkms A better example more similar to the OP's situation would be [Dictionary's indexer](http://msdn.microsoft.com/en-us/library/9tee9ht2%28v=vs.110%29.aspx) throwing a `KeyNotFoundException` when you try to retrieve a item that does not exist. – Scott Chamberlain Jun 19 '14 at 00:47
  • return null will throw me an error here: var Point = GetUIPos.GetUIPosition(name); while (Point[0] == 0 && Point[1] == 0) – George H. Jun 19 '14 at 00:48
  • @ileandros: that's correct. Hence you need to check if the result is not `null`. PS: also think of introducing (reusing) a separate type that represents a bounding box instead of this ugly array – zerkms Jun 19 '14 at 00:50
  • @ileandros, its the same check you would have needed to make if the `bool` code was compilable, you just have to make it against `null` instead of `false`. – BradleyDotNET Jun 19 '14 at 00:53
  • Something like this would fix it i guess: if(Point != null){} I didn't understand your second part though – George H. Jun 19 '14 at 00:54
  • @ileandros: the second part explanation: use a class to return the bounding box, not an array of doubles. Like this structure http://msdn.microsoft.com/en-us/library/system.drawing.rectangle.aspx PS: I'm not telling you to use this exact structure, it's just an example – zerkms Jun 19 '14 at 00:55
  • +1. Consider adding link to popular question on the topic too - http://stackoverflow.com/questions/748062/how-can-i-return-multiple-values-from-a-function-in-c (not exact duplicate, but covers many options). – Alexei Levenkov Jun 19 '14 at 00:56
  • @BradleyDotNET Yes, exactly. Didn't think that way. – George H. Jun 19 '14 at 00:56
  • @AlexeiLevenkov, added the link. Thanks for finding it! – BradleyDotNET Jun 19 '14 at 02:08