-1

If I have these two classes

class Car
{
    public int maxSpeed;
}

class Minivan : Car
{
    maxSpeed = 100; //error

    public void Test() 
    {
        maxSpeed = 100;//ok!
    } 
}

Why it's only permitted to use the public member of the base class within a method not the class body?

René Vogt
  • 43,056
  • 14
  • 77
  • 99
mshwf
  • 7,009
  • 12
  • 59
  • 133
  • 2
    if that line was be possible, when would you want that line to run? – pijemcolu Jul 02 '16 at 10:52
  • 1
    If you refer to a member outside of a method it is interpreted as an attempt to declare it, If you want to modify a member you have to do it inside a method. – John D Jul 02 '16 at 10:53
  • You probably want to do that in the constructor of the Minivan class – E. Mourits Jul 02 '16 at 10:54
  • Well, this is how C# works – meJustAndrew Jul 02 '16 at 10:57
  • What if I want to initialize it with a value? – mshwf Jul 02 '16 at 11:09
  • Put the initialisation in the constructor... – Charleh Jul 02 '16 at 11:28
  • You are the third person in less than two days to ask about putting *statements* inside of class bodies. Where are you getting the idea that this is acceptable? Is there some instructor or online tutorial that is doing an absolutely abysmal job? If you're trying to learn the language on your own, I recommend getting a book on C#, rather than just guessing. – Cody Gray - on strike Jul 02 '16 at 12:03
  • It's not even a question about inheritance (try to implement Minivan and Car in one class - it's not possible to put statements outside methods). You should learn C# basic syntax before thinking about inheritance. – Matt Jul 02 '16 at 15:12

3 Answers3

4

Why it's only permissible to use public member of the base class within a method not the class body?

You are thinking about this the wrong way.

It is not the accessibility rules that make this illegal; it would be illegal regardless of the accessibility.

The rule you are violating is that a statement must appear within the body of a method, property, constructor, destructor, indexer, operator or event. In your first code sample the statement appears outside of all of those, and in the second, it appears inside a method.

Again, this rule has nothing whatsoever to do with accessibility. It's a grammatical rule of the language that you are violating, not a semantic rule.

That should answer your question, but let's take a look at your code. The better way to write this would be:

abstract class Car 
{
  public abstract double MaximumSpeed { get; } 
}
class Minivan : Car 
{
  public override double MaximumSpeed { get { return 100.0; } }
}

Why is this better?

  • Public fields are a bad idea. Represent things that are logically properties as properties.
  • The property is now read-only. You don't want just anyone to be able to change the value!
  • Use good naming conventions.
  • Car is abstract, so you cannot create a vehicle that is just a car.
  • The maximum speed is a "physics" quantity, so it should be double, not int. There is no requirement that speed be quantized to an integer.
Eric Lippert
  • 647,829
  • 179
  • 1,238
  • 2,067
  • Are not properties, constructors, destructors, indexers, operators, and events just special cases of methods? Or does the C# spec actually treat them all as separate first-class entities? – Cody Gray - on strike Jul 03 '16 at 06:45
  • @CodyGray: As far as the syntax and semantics of C# go, they are different kinds of things. Of course as an *implementation detail*, they are all methods behind the scenes. – Eric Lippert Jul 03 '16 at 13:28
0
class Minivan : Car
{
maxSpeed=100; //error
}

Doesn't fit anywhere, it's not a member of the class, it doesn't even have a type. What is it?

If you want to initialize the property of base class in a child class you can use a constructor for that.

class Car
{
    Car()
    {
        maxSpeed = 100;
    }
}

If you need to pass a value for maxSpeed to every car you create you can create multiple constructors and chain them

Community
  • 1
  • 1
pijemcolu
  • 2,257
  • 22
  • 36
0

What you define in the class are the fields and what you declare inside the methods are the variables. They may look same but there is a difference between them. like you can't use access modifiers with variables but you can do with fields. Also there is a slit difference between fields and properties, Properties exposes fields marked private in a container like class. So you can not use them interchangeably, that means you can't use fields in place of variable. In your case maxspeed is a field and you are using it like a variable.

class Minivan : Car
    {
        //Inside a method
        public void Test()
        {
            maxSpeed = 100;
        }

        //Inside a constructor 
        public Minivan(int _maxSpeed)
        {
            maxSpeed = _maxSpeed;
        }

        //Inside a property
        public int MaxSpeed
        {
            get { return maxSpeed; }
            set { maxSpeed = value; }
        }
    }

So if you want to use the field use it inside a method or constructor or inside a property.Hope it helps. Please feel free to comment if any doubt.

Saket Choubey
  • 916
  • 6
  • 11