Help with C# Project - Supernatural Inheritance

Oh! I was nesting CastRainStorm() inside the constructor and that was what was causing the compiler to panic basically?

Idk, were you? What belongs where? What lets you answer that?

It’s not so much about knowing where things go but being deliberate about where things are placed.

“I’m going to add a method to my class” <- where does the method go?

Maybe you had

“I’m going to add this text from codecademy’s instructions into my file” <-- ehh. probably gonna do this wrong.
^ that’s something codecademy bombards you with, so the routing of your thoughts get a bit questionable

Yep, that was the issue all along! This is where I ended up at which is step 12. :smile:

// Mage.cs
using System;

namespace MagicalInheritance
{
  public class Mage : Pupil
  {
    public Mage(string Title) : base(Title) {}
    //Give the Mage a new spell: CastRainStorm().  Set to public and return type Storm. In the body of the method, construct a new Storm object and return it.  
    public Storm CastRainStorm()
    {
        //Construct a new Storm object and return it.
        Storm s = new Storm("rain", false, Title);
        return s;
    }
  }
}
using System;

namespace MagicalInheritance
{
  class Program
  {
    static void Main(string[] args)
    {
      //Test your class! In Program.cs in Main(), construct a new Storm.
      Storm s = new Storm("wind", false, "Zul'rajas");
      //After constructing the new object, call Announce() and print console.
      Console.WriteLine(s.Announce());
      
      //Construct a new Pupil named Mezil-kree.
      Pupil p = new Pupil("Mezil-kree");
      //Call CastWindStorm() and store in a variable.
      var Storm = p.CastWindStorm();
      // Use Announce() to check that it's a weak wind storm.
      Console.WriteLine(Storm.Announce());
      
      //Construct a new Mage named Gul’dan.
      Mage m = new Mage("Gul’dan");
      //Call CastRainStorm() and store in a variable. 
      var Rain = m.CastRainStorm();
      //Use Announce() to check that it is a weak rain storm.
      Console.WriteLine(Rain.Announce());
    }
  }
}```

Thank you for your help. It might of been cryptic and all, but it was enough to get me thinking and get me over the hill on this!

Zul’rajas cast a weak wind!
Mezil-kree cast a weak wind!
Gul’dan cast a weak rain!

I try to not treat symptoms. If I can solve a problem other people can too. It’s not always easy to figure out what’s missing though, or to address it and I’ll definitely botch it sometimes.

I was hoping you’d see it when I started talking about matching patterns, maybe that makes more sense now? Expressing intention in terms of the patterns known to the language, if one does that then one should end up writing only valid code, or be able to fix it… I think

2 Likes

The nesting bit was so obvious I just wasn’t seeing it as if I was looking right through it. The part that I had no idea about, and should of took the lessons words literally, was this part below. I had no idea this could be done. I just never done such a thing with an empty block of {}. It was the part I was suspicious about all along and couldn’t figure out why because I never seen this done before with an empty block. But now I get it, it was to inherit.

public Mage(string Title) : base(Title) {}

Hello, to anyone who’s stuck at step 7:

Give the Pupil its single spell: a CastWindStorm() method. It should be public with return type Storm . In the body of the method, construct a new Storm object and return it.

So far we’ve only instantiated objects from classes in the Main method, so it can get a little bit confusing to apply them to a method inside a class.

But remember that class is a custom data type, it can also be returned just like any other primitive types like int, string, bool, etc.

So we’ll get an object of the class Storm when we have this code:

class Pupil 
{
    /* omitted */
    public Storm CastWindStorm()
    {
        Storm s = new Storm("wind", false, Title);
        return s;
    }
}

You can call it in your Main method as below:

class Program 
{
    public static void Main(string[] args)
    {   
        Pupil p = new Pupil("Mezil-kree");
        Storm s = p.CastWindStorm(); // it returns the whole object so you can assign it to a variable with the same data type `Storm` 
        Console.WriteLine(s.Announce());
        // output: Mezil-kree cast a weak wind storm!
    }
}

The difference is that the class Storm gathers different information in different classes.
This project is a good example of reusing classes to avoid duplicate codes.

Repeat the same thing in Mage and Archmage and you should be able to make it on your own to step 18.

I hope this helps. :blush:

And… Please correct me if I got anything wrong since I’m pretty new to C#. :laughing:

Hi

My finished project Supernatural Inheritance using c#

cheers