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

时间:2013-12-08 16:37:33

标签: c# parameters ref

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

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

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

你知道我怎么能解决这个问题吗?

非常感谢任何帮助。

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

使用System;

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);
    }




}

}

2 个答案:

答案 0 :(得分:2)

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

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

答案 1 :(得分:0)

导致问题的代码部分是您为变量分配了void方法(在这种情况下,这是分配给CalcPrice()方法的{totalPrice“变量:totalPrice=CalcPrice(...)。仅将方法分配给变量的事实意味着C#编译器希望该方法返回一个值;但是您的方法不能,因为它是一个void方法。 您有两种选择,一种是通过将方法转换为一个将值返回给调用方法的方法来重做该方法,或者两种是删除方法对变量的赋值,然后在假设您的“ ref变量”的情况下进行编码将通过void方法进行适当更改。