-8

I am trying to use boolean when overriding. It all works but the boolean one

public Bike(String colour, int gears, Boolean bell) : base (colour)

Bike B1 = new Bike("red ", 5, No);
        //B1.Name = "Red";
        Console.WriteLine("" + B1.ToString());
        //Console.WriteLine("Name: " + B1.Name);

The sentence I am trying to create is The red bike has 5 gears and no bell

The "no" bit is the boolean one so how do you use boolean using the word No because when I have it on the program like above "Bike B1 = new Bike("red ", 5, No);" I get an error saying "The name 'No' does not exist in the current context"

1
  • 3
    bools can only accept true or false literals. Did you think No was a valid boolean value?
    – sstan
    Aug 17, 2015 at 15:38

2 Answers 2

2

No is not a keyword in C#. For bool type, use true or false

1
  • You're missing a tick towards the beginning of that line, inverting what you meant to do :)
    – bkribbs
    Aug 17, 2015 at 15:40
0

I think that your problem is that you are not understanding well data type.
Please to read about the boolean bool (C# Reference).
This said: "The bool keyword is an alias of System.Boolean. It is used to declare variables to store the Boolean values, true and false."
If you require a Boolean variable that can also have a value of null, use bool?. you may try something like this

public Bike(String colour, int gears, Boolean bell)
                : base (colour){
 ////.......
public override ToString(){
    string yesNo=(bell==true)? "with one":"and no";
    return string.Format("The {0} bike has {1} {2} bell", colour, gears, yesNo);

}
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.