0
using System;

namespace MyProgram
{
  class Point
  {
      public readonly int X;
      public readonly int Y;

      public Point (int x, int y)
      {
        X = x;
        Y = y;
}

^ That is the parent class

namespace MyProgram
    { 
      class MapLocation : Point

      {
          public MapLocation(int x, int y, Map map) : base(x, y)

^ This is the child class

Basically, my question is: How does the passing of parameters between classes work? What is an easy way to remember the rules of passing parameters around? And also, where it says public MapLocation(int x, int y, Map map) : base(x, y), I don't understand why it's just x, y and not 'int'?. C# is becoming very frustrating and I'm not having very much fun :( Help would be appreciated.

Cole Connelly
  • 333
  • 1
  • 3
  • 7
  • What exactly do you mean by the rules? `base()` is invoking the parent's constructor. It's just `x,y` rather than `int x, int y` for the same reason you write `DoSomething(x)` rather than `DoSomething(int x)`. – Rob Aug 30 '17 at 01:52
  • `: base(x, y)` is simply method invocation, that is why you don't need to declare types again, they are already declared in constructor of base class – Fabio Aug 30 '17 at 01:59
  • put `var m = new MapLoaction(1,2,someMap);` and debug it - this should clarify some things. – tymtam Aug 30 '17 at 02:15

6 Answers6

0

You are defining MapLocation(int x, int y, Map map) so you need to indicate the types. You can say they are parameters. You are calling base(x, y), so x, y are arguments. Ref: What's the difference between an argument and a parameter?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Kevin .NET
  • 463
  • 6
  • 9
0

The line of code public MapLocation(int x, int y, Map map) : base(x, y) means, when constructing a MapLocation given integers x, y, and Map map, call the constructor for the base class (Point) with the values of x and y.

You aren't passing parameters between classes per se, you are more properly calling the constructor of the base class.

Tim
  • 5,940
  • 1
  • 12
  • 18
0

base(x, y) doesn't say int x, int y because it is not a method/function declaration, base is calling the base class constructor passing x and y that are arguments provided to the MapLocation constructor. So the parameters are declared int, as you expect, in the MapLocation constructor.

How do you remember rules or passing parameters around you ask. Outside of basic simple stuff that doesn't hurt the brain, you don't.

If you understand that x and y are parameters of type intdeclared for the MapLocation constructor, and you understand that you can call the base class constructor in C# with : base(). Then it is easy to remember whatever is passed to the child class constructor is available to pass to the parent constructor, via base in C#, so in your code example : base(x, y)

base is simply syntactic sugar to call a parent class constructor in C#. base arguments must match what the parent class parameters defined in the constructor, so in your code sample Point(int x, int y), base(x, y) is a quicker way of saying new Point(x,y).

Brian Ogden
  • 18,439
  • 10
  • 97
  • 176
0

What is an easy way to remember the rules of passing parameters around?

C# is strong typed language, so types provides rules of how passing parameters to their methods.
All modern IDE, which used for writing C#, will provide you information about what parameters should be passed to the method. So, actually in Visual Studio, Intellisense will tell you that rules. Using IntelliSense.
Compiler will not be able to compile code if parameters of wrong type provided or some parameters missing.

And also, where it says public MapLocation(int x, int y, Map map) : base(x, y), I don't understand why it's just x, y and not 'int'?

: base(x, y) is simply method invocation, that is why you don't need to declare types again, they are already declared in constructor of base class.

Fabio
  • 31,528
  • 4
  • 33
  • 72
0

I don't understand why it's just x, y and not 'int'?

Because you are calling the base constructor. Don't think of this just as part of the child class definition... this is part of the implementation now. When you say base(x,y), you have the same syntax you would have for calling any other function.

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
0

where it says public MapLocation(int x, int y, Map map) : base(x, y), I don't understand why it's just x, y and not 'int'?.

Imagine you have this:

public class ClassOne
{
    public int MethodOne(int x, int y)
    {
        return x + y;
    }
}

Now let's call MethodOne:

ClassOne one = new ClassOne();
int answer = one.MethodOne(2, 3); //<== see this, now read note below
int first = 2;
int second = 3;
int answer2 = one.MethodOne(first, second); //<== see this, now read note below

When you are calling a method, you pass arguments to it: The type of arguments do not need to be specified because they will be inferred from the type of what they are. For example, in the above we simply pass 2, and 3 so the compiler knows these are int constants. In the second call, they are the type of first and second which are both ints.

When you call a base constructor like this: base(x, y), it is the same idea as when you call a method as I have explained above. The type of x and y are inferred from: MapLocation(int x, int y, .... If you change it to MapLocation(string x, int y, ... then the compiler will complain. Try it and see.

Now imagine we have a type which is not the type the method is expecting as shown below:

long x = 1;
int y = 2;

Now x is of type long but MethodOne expects an integer, so we need to cast x to an int (note that not every long will fit in an int so this can fail). Here is how we would do this:

one.MethodOne((int)x, y); //<== see the casting
CodingYoshi
  • 25,467
  • 4
  • 62
  • 64