Help with C# Project - Supernatural Inheritance

Link to the project URL

I am stuck on this project on a specific error.

Mage.cs(10,7): error CS0106: The modifier ‘public’ is not valid for this item [/home/ccuser/workspace/learn-csharp-interfaces-inheritance-csharp-supernatural-inheritance/MagicalInheritance.csproj]

I am unable to resolve this error after reading this page

Link to the MS Page for Compiler Error CS0106

This is the script that throws the error.

// Mage.cs
using System;

namespace MagicalInheritance
{
  public class Mage : Pupil
  {
    public Mage(string title) : base(title)
    {
      public Storm CastRainStorm()
      {
        //Construct a new Storm object and return it.
        Storm s = new Storm("rain", false, Title);
        return s;
      }
    }
  }
}

I can post the other scripts if needed. Please help.

Maybe add one thing at a time, compiling after each addition, and if something goes wrong you can reconsider what you were doing and what that needs to look like.

I imagine your code might compile if you remove your CastRainStorm which is what your error message is pointing at, and then you can continue from there.

I do hit save (compile) after each change. Yes, if I remove that line it compiles. However, in the project they want you to add that. So, that is where I am stuck.

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

So you’re adding a method for some class? Which class? Where would a method for a class go?

That’s something you can look up one way or another, that’s what you would need to consider for the thing you’re adding.

What did you put your method inside, does it go there?

Are you being rhetoric to make me think about that, or are you asking and need me to post more code @ionatan?

First option.

Make sure your lego bricks fit together.

class Something {
    public int MyMethod() {
    }
}

Adding a method to a class looks something like that (I don’t write csharp so it’s probably somewhat wrong but close enough), right. But that’s not the modification you made.

I’ll think about that some more, but I’m stuck on trying to figure out why its telling me this.

Mage.cs(10,7): error CS0106: The modifier ‘public’ is not valid for this item [/home/ccuser/workspace/learn-csharp-interfaces-inheritance-csharp-supernatural-inheritance/MagicalInheritance.csproj]

Basically, I don’t know why its not valid for “this item”.

These are the reasons MS tells me, and from it I can’t make sense of what went wrong?

The modifier ‘modifier’ is not valid for this item

A class or interface member was marked with an invalid access modifier. The following examples describe some of these invalid modifiers:

  • The static and public modifiers are not permitted on interface methods.
  • The static modifier is not permitted on a local function.
  • The public keyword is not allowed on an explicit interface declaration. In this case, remove the public keyword from the explicit interface declaration.
  • The abstract keyword is not allowed on an explicit interface declaration because an explicit interface implementation can never be overridden.
  • Access modifiers are not allowed on a local function. Local functions are always private.

In prior releases of Visual Studio, the static modifier was not permitted on a class, but static classes are allowed starting with Visual Studio 2005.

However, the step is asking me to make it public

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

But MS is telling me that is not permitted?

The way that code files are generally read (parsed) is by matching everything in the code file to known patterns (syntax/grammar)

So long as you’re still matching known patterns, everything is fine, but if the parser encounters something that doesn’t match anything then it’ll stop reading and report the location where it stopped reading

If a parser error doesn’t make too much sense to you and you feel like you know what you’re doing somewhat in general, then forget about the message and instead think about how that all should have fit together.

And this is definitely such a case, the compiler knows nothing about what you mean/want so it can never meaningfully tell you how something should be.

OK, so I’m going to backtrack a step, since I am suspicious of my work on the previous step.

Add a Mage constructor with one string argument. It should call the base constructor with that same argument.

Is the following code correct in what it is asking for me to do? I feel like if this was incorrect then everything in scope is the reason for failing.

// Mage.cs
using System;

namespace MagicalInheritance
{
  public class Mage : Pupil
  {
    public Mage(string title) : base(title)
    {

    }
  }
}

Yes it compiles, but I’m wondering if this step was done correctly, leading to the reason I am failing the next step?

Hmm. Well, how would we measure that?

Behaviour would be good to test, I think? Does that have the desired behaviour?

... Main ... {
    new Mage()  // does this invoke the body of the constructor?
}

Whether it compiles is a pretty good indicator here, there aren’t a whole lot of possible valid things you could do.

Maybe the compiler is the only thing you care about satisfying right now.

Your class should have a constructor, and a method. They both go into the class don’t they? So you’d put both in the class. Done. (That is how these lego blocks fit isn’t it, that’s what we need to be doing in this situation)

You can also try writing the code without the constructor, and just the method for now. If you can get each of them to work on their own, how should they look together?

My suspicion lies with the use of “Title” or “title”. I can’t tell which one I should use. I can’t tell from the compiler messages if either matters. I just know that “Title” is inherited from “Pupil” (I think?). But when I reference the hint, it hints to use lowercase title, similar to how its also seen in the Pupil class.

Basically I’m just getting more confused at this point now :confused:

// Pupil.cs
using System;

namespace MagicalInheritance
{
  public class Pupil
  {
    public string Title { get; private set; }
   //Cconstructor with one argument, that sets the Title property 
    public Pupil(string title)
    {
      Title = title;
    }
    //Give the Pupil its single spell: CastWindStorm() Public, return type Storm. In the body of the method, construct a new Storm object and return it.
    public Storm CastWindStorm()
    {
      Storm s = new Storm("wind", false, Title);
      return s;
    }
  }
}```

If the compiler doesn’t complain it doesn’t care. I don’t know csharp naming conventions other than that a whole lot of things are title cased where they aren’t in other languages but that says nothing about your case.
Do arrays have a size attribute? Is that title cased? That’d be the equivalent thing.

Also, your Mage.cs isn’t really any different from Pupil.cs, so, how did you do the same thing there?

If you mean the multiple variables then obviously you’ll need to keep in mind which one of them you mean and possibly disambiguate if the names collide in some way

Point of the lesson (I gather) is that each one is going to inherit from the other. So, what (I assume) you do in one wont necessarily be what you might do in another due to inheritance. Reason the other works (I assume) and this one does not, despite them seemingly being the same, appears to me due to again, inheritance. That is why I was suspicious of these two lines being wrong, yet compiling because it satisfies the compiler. I assume the Mage inheriting Pupil was done correctly. I’m still suspicious of the latter. I’m mostly confident this was done correctly

In Mage.cs , define an empty Mage class that inherits from Pupil .

So its the next step that has me curious.

Add a Mage constructor with one string argument. It should call the base constructor with that same argument.

  public class Mage : Pupil
  {
    public Mage(string title) : base(title)
    {

    }
  }```

Hmm, I don’t know why I didn’t try this, and wondering if this change is going to lead me down the right path.

// Mage.cs
using System;

namespace MagicalInheritance
{
  public class Mage : Pupil
  {
    Mage(string title) : base(title)
    {

    }
  }
}```

Remove the inheritance while you fix it then. But no it makes no difference to syntax (why would it)

I don’t know why it would or wouldn’t. That’s how new I am to C#.

So anyway to skirt around it a bit less.

You’d start with this, yes?

class Something {



}

Then shove in the things that should be in it.

class Something {
   

  // put a constructor here




  // put a method here


}

You have a class. In the class you’d put contstructors, properties, fields, methods.

Yes, that’s what you’re doing wrong, trivial as it may seem.

That is the pattern that you’re looking to work with here. Right?
classname … open brace … zero or more of methods/constructors/properties one after the other … close brace.