Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Noob Concern: Problems making a new void method. C#

Tags:

c#

I am trying to make a method in a new class that I made...

public void CalcDrinks(bool HealthOption) 
{
    if (HealthOption)
    {
        CostOfBeverage = 5M; 
    }
    else
    {
        CostOfBeverage = 20M; 
    }
}

And I keep getting a red squiggly under the void saying... "expected class, delegate, enum, interface or struct error"

I'm not sure what I am missing...

like image 518
MonuMan5 Avatar asked Aug 11 '26 14:08

MonuMan5


1 Answers

You would get that precisely that error if the method is declared outside of a class.

namespace Blah
{
    public void CalcDrinks(bool HealthOption) 
    {
        if (HealthOption)
        {
            CostOfBeverage = 5M; 
        }
        else
        {
            CostOfBeverage = 20M; 
        }
    }
}

In this snippet, there is no class definition to be seen. Fix it to the below and see that it compiles.

public class Foo
{
    private decimal CostOfBeverage;

    public void CalcDrinks(bool HealthOption)
    {
        if (HealthOption)
        {
            CostOfBeverage = 5M;
        }
        else
        {
            CostOfBeverage = 20M;
        }
    }
}
like image 61
Anthony Pegram Avatar answered Aug 13 '26 04:08

Anthony Pegram



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!