LoadContent not recognized in c# class file

When I add a new file by "Project->Add Class->Visual C# items->Add class It opens a new file with the name I supply. Then I add the code below.

`using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Input;

namespace MultiFile
{
///


/// A general class to build a Game Hero
///

public class Hero
{
#region Fields

    Texture2D sprite;
    Rectangle DrawRectangle;

    #endregion

    #region Constructors
    /// <summary>
    ///
    /// </summary>
    /// <param name="contentManager"></param>
    /// <param name="spriteName"></param>
    /// <param name="x"></param>
    /// <param name="y"></param>
    public Hero(ContentManager contentManager, string spriteName, int x, int y)
    {
        LoadContent(contentManager, spriteName, x, y);
    }
    #endregion

}

}`

When I call it from the main game class like this

` protected override void LoadContent()
{
// Create a new SpriteBatch, which can be used to draw textures.
spriteBatch = new SpriteBatch(GraphicsDevice);

        // TODO: use this.Content to load your game content here
        hero = new Hero(Content, (@"idle"), graphics.PreferredBackBufferWidth / 2,
            graphics.PreferredBackBufferHeight / 2);

It does NOT show any errors BUT when I call

` protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);

        spriteBatch.Begin();

        hero.Draw(spriteBatch);


        spriteBatch.End();

        // TODO: Add your drawing code here

        base.Draw(gameTime);
    }`

an error is thrown saying

Error CS1061
‘Hero’ does not contain a definition for ‘Draw’ and no extension method ‘Draw’ accepting a first argument of type ‘Hero’ could be found (are you missing a using directive or an assembly reference?)
MultiFile D:\C#\MY APPS\MultiFile\MultiFile\Game1.cs
92 Active
I am very new to C# and Visual studio.

Also I have noticed that in some classes the word “public” is colored blue but in mine it is grey.

The error is telling you exactly what’s wrong. The Hero class you created does not contain a method (public void) named Draw.

To fix it, add this code to your Hero class:

public void Draw(SpriteBatch spriteBatch)
{
  //Hero drawing code here
}
1 Like

Thanks for that! There is also a red squiggly line under the LoadContent in
#region Constructors
///


///Constructs the Hero
///

///
///
///
///
public Hero(ContentManager contentManager, string spriteName, int x, int y)
{
LoadContent(contentManager, spriteName, x, y);
}
#endregion
and when i hover my mouse over that it says “The name LoadContent does not exist in the current context”. Why is that?

This is another case of “You don’t have this method declared in this class.”

Remember that your Game1 and Hero files are different classes. You can’t call the functions in Game1 directly from the Hero class and vice versa.

Since your Hero constructor public Hero asks for a ContentManager, you can use said ContentManager to load in the content you need for the Hero (just like you would in Game1's LoadContent().

This would be where you would populate the sprite variable (and other resources in your Hero class).

For example:

public Hero(ContentManager contentManager, string spriteName, int x, int y)
{
    LoadContent(contentManager, spriteName, x, y);
}

would become

public Hero(ContentManager contentManager, string spriteName, int x, int y)
{
    sprite = contentManager.Load<Texture2D>(spriteName);
    DrawRectangle = new Rectangle(x, y, sprite.Width, sprite.Height);
}

Also, as a good citizen, I’d recommend adding a finalizer to your Hero class that disposes any resources you load inside the constructor. You can do this by adding

~Hero()
{
    sprite.Dispose(); //Release any memory that the sprite's taking up so it can be used for other things
    sprite = null; //De-allocate the sprite object so it can't be used anymore in this Hero instance.
}

to your Hero class. This’ll get called when C#'s garbage collector kicks in and deallocates any unused Hero classes. If you don’t do this, the sprites associated with said Heroes will never be de-allocated and they’ll just be sitting there taking up precious RAM. This is what’s called a memory leak and is something you do not want to have in a game because if it’s bad enough you’ll end up with performance loss, bad framerates, and OutOfMemoryExceptions.

P.S. Just to make your posts on the forum a bit more readable, when you’re posting code, try to surround it with three backticks at the top and at the bottom of the code you’re posting. Additionally you can also add the word “csharp” next to the first three backticks so you get C# syntax highlighting in your code, but that’s optional. For example:

public void someCode()
{
  //Have some code :D
}

1 Like

You are absolutely correct! Many thanks and many merits and May You be Well. :slight_smile:

Watercolor_Games, Your suggestions cleared all my errors. Take a look at this code it works differently, like the code that prduced my errors, but compiles well and runs properly. Why is that?
Game1.cs
Burger.cs

If you scroll down a fair bit in Burger.cs, you’ll see that it declares its very own LoadContent method that any Burger can call to load in its content.

However, your Hero class didn’t declare such a method, thus it was causing the errors because C# was going “W…what? What’s LoadContent()? I don’t see what you mean.”

Burger also declares its own Draw() method, which can be accessed by any Burger or anyone who creates a variable of type Burger. Your Hero class was also missing such a method causing the second error.

I’ll link you to a series called Codegasm by Barnacules Nerdgasm. He’s a former Microsoft employee and his Codegasm series is all about C# from a beginner’s standpoint.

A lot of the videos in that series will teach you some very useful tips and tricks that you’ll use all the time even in MonoGame, one of which being the concepts of classes/methods/fields/properties etc. His videos are long but they’re pretty easy to understand and all his code is open-source so you can reference it at any time.

1 Like

Hey thanks a million! Barnacules Nerdgasm and his Codegasm are way up my street!!! Thanks for sharing. Many merits. May you be well.