简体   繁体   English

如何从具有不同类型参数的函数返回浮点数?

[英]How can I return a float from a function with different type arguments?

I have this function and I want to pass two Vector3 arguments and one int so I can return a float value. 我有此功能,我想传递两个Vector3参数和一个整数,以便返回浮点值。 How can I return a float value even tho I use Vector3 and int as arguments? 即使使用Vector3和int作为参数,如何返回浮点值? This is my code: 这是我的代码:

//find other types of distances along with the basic one
public object  DistanceToNextPoint (Vector3 from, Vector3 to, int typeOfDistance)
{
    float distance;
    //this is used to find a normal distance
    if(typeOfDistance == 0)
    {
        distance = Vector3.Distance(from, to);
        return distance;
    }
    //This is mostly used when the cat is climbing the fence
    if(typeOfDistance == 1)
    {
       distance = Vector3.Distance(from, new Vector3(from.x, to.y, to.z));
    }
}

When i replace the "object" keyword with the "return" keyworkd it gives me this error; 当我用“ return”键替换“ object”关键字时,出现此错误; enter image description here 在此处输入图片说明

Two problems with your code. 您的代码有两个问题。

  1. If you want to return an object type then you need to cast your result to float before using it. 如果要返回对象类型,则需要在使用它之前将结果强制转换为浮点型。
  2. Not all code paths return a value. 并非所有代码路径都返回值。

You can try this: 您可以尝试以下方法:

/// <summary>
/// find other types of distances along with the basic one
/// </summary>
public float DistanceToNextPoint (Vector3 from, Vector3 to, int typeOfDistance)
{
    float distance;

    switch(typeOfDistance)
    {
        case 0:
             //this is used to find a normal distance
             distance = Vector3.Distance(from, to);
        break;
        case 1:
             //This is mostly used when the cat is climbing the fence
             distance = Vector3.Distance(from, new Vector3(from.x, to.y, to.z));
        break;
    }

   return distance;
}

The changes include: 更改包括:

  • Return float type instead of object 返回浮点类型而不是对象
  • Make sure all code paths return a float type 确保所有代码路径都返回浮点类型
  • Reorganize to use a switch 重新组织以使用开关

just change return type from object to float like this: 只需将返回类型从object更改为float如下所示:

public object  DistanceToNextPoint(...)

To: 至:

public float  DistanceToNextPoint(...)

then return your variable distance in the last line of your method: 然后在方法的最后一行返回您的可变distance

public float DistanceToNextPoint(...){
    // ...
    return distance:
}

You should change the return type object to float . 您应该将return类型object更改为float

    //finde other tipe of distances along with the basic one
    public float DistanceToNextPoint (Vector3 from, Vector3 to, int tipeOfDistance)
    {
        float distance;
        //this is used to find a normal distance
        if(tipeOfDistance == 0)
        {
            distance = Vector3.Distance(from, to);
            return distance;
        }
        //This is mostly used when the cat is climbing the fence
        if(tipeOfDistance == 1)
        {
           distance = Vector3.Distance(from, new Vector3(from.x, to.y, to.z))
           return distance;
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在不同的返回类型函数中返回错误消息? - How do i return error message in different return type function? 我可以从泛型类中的函数返回不同的类型,具体取决于用于类的泛型类型是值还是引用类型? - Can I return a different type from a function in a generic class depending on if the generic type used for the class is a value or a reference type? 如何从具有通用返回类型的方法返回原语? - How can I return a primitive from a method with a generic return type? 我如何乘以浮点数和泛型类型? - How can I multiply a float and a generic type? 如何从接受参数的控制器返回不同的ActionResult? - How do I return a different ActionResult from my controller that takes arguments? 我如何创建ac#方法,该方法将返回与作为参数的具有相同元素类型的相同集合类型? - How can i create a c# method that will return the same collection type as as argument with a different element type? 如何从Func委托返回T类型? - How can I return type T from Func delegate? 如何从方法返回匿名类型? - How can I return an anonymous type from a method? 我如何从IList获得float [] <float> 与xamarin android吗? - How can I have a float[] from an IList<float> with xamarin android? 如何撤消浮点数中的数字 - How can I undo a number from a float
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM