首页 文章

使用带有ref参数的类型void方法,不能隐式地将类型void转换为double

提问于
浏览
0

我是C#的新手,并且在使用不会向Main()方法返回值的引用参数(静态void类型)时遇到大麻烦 . 我的编译器给出了以下错误消息:无法隐式将void类型转换为double . 我已经研究了这个网站(和其他地方)的类似问题,但只能找到一个类似的情况,其中错误消息涉及隐式地将void转换为int . Cannot implicitly convert type void to int

提供的解决方案涉及使方法返回一个值带,从方法头中删除void .

这就是我的问题开始的地方 . 我需要使用带有ref参数的名为CalcPrice的void方法 . 该方法执行一个应该返回到Main()方法的计算,但它没有这样做 . 我试图将CalcPrice分配给Main()方法中的变量,但这会生成我的错误消息:无法隐式地将类型void转换为double .

你知道我怎么能绕过这个吗?

任何帮助将不胜感激 .

请参阅下面的代码 . 谢谢彼得

使用系统;

public class TESKDESK
{
    public static void Main()
    {
        // Declare and initialize variable for price and discount rate
        int numberOfDrawers;  
        //this variable references the OUT paramater and does not need to be   
        assigned  a value
        char typeOfWood; //this variable references the OUT paramater and does not 
        need to be assigned a value
        //int myDrawers = 0;

        //char myWood = ' ';
        double totalPrice=0;
        //discount = 0.10,
        //discountAmount;
        //GetDrawers(numberOfDrawers);

        //Call the GetDrawers() Method which prompts customer for number of drawers, 
        accepts the data entry,
        //then returns that number to the Main() method and stores it in a new 
        variable called myDrawers 
        //myDrawers = GetDrawers(numberOfDrawers);
        GetDrawers(out numberOfDrawers);


        //Call the GetWood() Method which prompts customer for their choice of desk 
        wood, accepts the data entry,
        //then returns that character to the Main() method and stores it in a new 
        variable called myWood
        //myWood = GetWoodType(typeOfWood);
        GetWoodType(out typeOfWood);
        //Console.WriteLine("You choose {0}", myWood);

        //Call the CalcPrice() Method which takes in number of drawers and desk wood 
        choosen by the customer,
        //performs a calculation with the values, then returns the final cost to a new 
        variable in Main() Method called totalPrice
        totalPrice=CalcPrice(ref numberOfDrawers, ref typeOfWood);

        DisplayResults(numberOfDrawers, typeOfWood, totalPrice);

    }

    // Create method to receive the number of desks the customer requires using the 
    OUT paramater, but the method will NOT return the value to the Main() method
    public static void GetDrawers(out int numberOfDrawers)
    {

    int myDrawers = 0;
    Console.Write("Enter the number of drawers: ");
    numberOfDrawers = Convert.ToInt16(Console.ReadLine());


    // Return result
    //return myDrawers;
    }

    //Create method to receive the customer's choice of wood product using the OUT 
paramater, but the method will NOT return the choice to the Main() method
public static void GetWoodType(out char typeOfWood)
{

    //bool acceptWoodChoice = false; //Wood product choice can be accepted
    char woodChoice = ' ';
    char pine = 'p'/*,
         oak = 'o',
         mahogany = 'm'*/;

    Console.Write("Enter the type of wood:  m, o, or p: ");
    typeOfWood = Convert.ToChar(Console.ReadLine());

    if (typeOfWood == 'p')
        Console.WriteLine("This confirms you ordered {0}", typeOfWood);
    else
        Console.WriteLine("Not recognized");


    //Create and assign values to a list of wood types



    //Return customer's choice of wood product the to the Main() method
    //return woodChoice;
    }

    //Create a method to receive drawers and wood product data entry by the REF 
 paramater and calculate price, but the method will NOT return the value to the 
Main() method
private static void CalcPrice(ref int numerOfDrawers, ref char typeOfWood)
{
    //Create and assign variables
    double totalPrice = 0;

    const double SURCHARGE = 30.00;

    if (typeOfWood == 'p')
    {
        totalPrice = (numerOfDrawers * SURCHARGE) + PINE;

    }
    /*else if (typeOfWood == 'o')
    {
        totalPrice = (numerOfDrawers * SURCHARGE) + OAK;
    }
    else
        totalPrice = (numerOfDrawers * SURCHARGE) + OTHER;*/



    //return totalPrice;

    }

     //Create a method to receive drawers and wood product data entry and calculate 
 price private static void DisplayResults(int numerOfDrawers, char typeOfWood, double 
 totalPrice)
{



    //Summarize wood product and drawers selected for customer


    if (typeOfWood == 'p')
    {
        Console.WriteLine("\nYou have ordered a Pine desk with {0} drawers", numerOfDrawers);
        Console.WriteLine("Total Cost is {0:C}\n", totalPrice);
    }




}

}

1 回答

  • 2

    正如编译器试图告诉你的那样, CalcPrice() 不会返回任何内容 .
    totalPrice=CalcPrice(...) 没有任何意义 .

    相反,您应该更改方法以返回 double 并使用 return 语句 .
    (或者,您可以使用更多 out 参数)

相关问题