0

I'm trying to figure out simply pass arguments from one constructor to another. In this case I want to know how to pass the parameters int protons,int electrons from class Atom to my derived class called hydrogen where my constructor is. Right now I'm currently using int,int in my derived constructor inside of Hydrogen which seems to be incorrect.

using System;

public class Atom
{
public Atom (int protons, int electrons)
{
    Protons = protons;
    Electrons = electrons;
}

public int Protons { get; set; }

public int Electrons { get; set; }

public void Send()
{
    Console.WriteLine(Protons);
    Console.WriteLine(Electrons); 
}
}

public class Hydrogen : Atom
{
    public Hydrogen() : base(int, int) { }   <!--This is the line that is wrong -->
}

public class Program
{
    public static void Main()
    {
        var hydrogen = new Hydrogen();
        hydrogen.Send();
    }
}

I would like the above code to compile but I'm getting an error in my derived class where I'm passing through int, int next to the : base. I've tried passing through protons and electrons but that doesn't compile either.

Camilo Terevinto
  • 31,141
  • 6
  • 88
  • 120
evan
  • 87
  • 1
  • 9
  • 1
    You use `public Hydrogen(int protons, int electrons) : base (protons, electrons)` – Camilo Terevinto Dec 09 '17 at 20:47
  • That doesn't seem to work when i test it either – evan Dec 09 '17 at 20:49
  • 2
    ... or public Hydrogen():base(1,1) {} – Sir Rufo Dec 09 '17 at 20:50
  • 2
    @evan look at this link for explanation on why to use the key word `base` also google C# msdn topic on `base` while you're at it https://stackoverflow.com/questions/2644316/what-really-is-the-purpose-of-base-keyword-in-c – MethodMan Dec 09 '17 at 20:51
  • Thanks guys @sir Rufo I tried this and it worked. It's all very new to me – evan Dec 09 '17 at 20:53
  • @evan You should take some time to read the docs, a c# book for beginners and some tutorials to get the basics – Sir Rufo Dec 09 '17 at 20:58
  • Your base class expects 2 integer values. If you were to call a method expecting 2 integer values you would not do this: `Foo(int, int)` so the same goes for why you cannot do this: `base(int, int) `. You need to pass an integer value not integer type like this: `base(1 /*or any other number*/, 1)` – CodingYoshi Dec 09 '17 at 21:05

0 Answers0