简体   繁体   中英

C# return bool from double[] method

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?

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#?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM