Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/264.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 无法使用带有ref参数的类型void方法将类型void隐式转换为double_C#_Parameters_Ref - Fatal编程技术网

C# 无法使用带有ref参数的类型void方法将类型void隐式转换为double

C# 无法使用带有ref参数的类型void方法将类型void隐式转换为double,c#,parameters,ref,C#,Parameters,Ref,我是C#新手,在引用参数(static void type)时遇到了很大的问题,因为它不会向Main()方法返回值。我的编译器给出以下错误消息:无法隐式将类型void转换为double。我研究过这个站点(和其他地方)的一个类似问题,但只能找到一个类似的情况,其中错误消息涉及隐式地将void转换为int 提供的解决方案涉及使方法返回一个值带,从而从方法头中删除void 这就是我的问题开始的地方。我需要使用一个名为CalcPrice的void方法,并带有ref参数。该方法执行一个本应返回Main()

我是C#新手,在引用参数(static void type)时遇到了很大的问题,因为它不会向Main()方法返回值。我的编译器给出以下错误消息:无法隐式将类型void转换为double。我研究过这个站点(和其他地方)的一个类似问题,但只能找到一个类似的情况,其中错误消息涉及隐式地将void转换为int

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

这就是我的问题开始的地方。我需要使用一个名为CalcPrice的void方法,并带有ref参数。该方法执行一个本应返回Main()方法的计算,但结果并非如此。我试图在Main()方法中将CalcPrice赋值给一个变量,但这会生成错误消息:无法隐式地将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);
    }




}

}

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

相反,您应该将方法更改为return
double
,并使用
return
语句。

(或者,您可以使用更多的
out
参数)

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

相反,您应该将方法更改为return
double
,并使用
return
语句。

(或者,您可以使用更多的
out
参数)

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

相反,您应该将方法更改为return
double
,并使用
return
语句。

(或者,您可以使用更多的
out
参数)

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

相反,您应该将方法更改为return
double
,并使用
return
语句。

(或者,您可以使用更多的
out
参数)

导致问题的代码部分是您将void方法分配给变量的地方(在本例中,是分配给CalcPrice()方法的“totalPrice”变量:
totalPrice=CalcPrice(…)
。将方法赋给变量这一事实意味着C编译器希望该方法返回一个值;但您的方法不能,因为它是一个void方法。
您有两种选择,一种是通过将方法转换为向调用方法返回值的方法来返工该方法,另一种是删除方法对变量的赋值,然后假设“ref variable”将被void方法正确更改。

导致问题的代码部分是您将void方法分配给变量的地方(在本例中,是分配给CalcPrice()方法的“totalPrice”变量:
totalPrice=CalcPrice(…)
。将方法赋给变量这一事实意味着C编译器希望该方法返回一个值;但您的方法不能,因为它是一个void方法。
您有两种选择,一种是通过将方法转换为向调用方法返回值的方法来返工该方法,另一种是删除方法对变量的赋值,然后假设“ref variable”将被void方法正确更改。

导致问题的代码部分是您将void方法分配给变量的地方(在本例中,是分配给CalcPrice()方法的“totalPrice”变量:
totalPrice=CalcPrice(…)
。将方法赋给变量这一事实意味着C编译器希望该方法返回一个值;但您的方法不能,因为它是一个void方法。
您有两种选择,一种是通过将方法转换为向调用方法返回值的方法来返工该方法,另一种是删除方法对变量的赋值,然后假设“ref variable”将被void方法正确更改。

导致问题的代码部分是您将void方法分配给变量的地方(在本例中,是分配给CalcPrice()方法的“totalPrice”变量:
totalPrice=CalcPrice(…)
。将方法赋给变量这一事实意味着C编译器希望该方法返回一个值;但您的方法不能,因为它是一个void方法。

您有两种选择,一种是通过将方法转换为向调用方法返回值的方法来返工该方法,另一种是删除方法对变量的赋值,然后假设“ref variable”将由void方法正确更改。

不要使用
out
parameters@SLaks-出于好奇,为什么?@DeeMac:它们令人困惑且烦人冗长。你也不应该在这里使用
ref
参数。@SLaks-它们有自己的位置。不要使用
out
parameters@SLaks-出于好奇,为什么?@DeeMac:他们很困惑唱歌,令人厌烦地冗长。你也不应该在这里使用
ref
参数。@SLaks-它们有自己的位置。不要使用
out
parameters@SLaks-出于好奇,为什么?@DeeMac:它们令人困惑且烦人冗长。你也不应该在这里使用
ref
参数。@SLaks-它们有自己的位置。不要使用