How to use GraphicsDeviceManager in different class?

I have a class with a method to create a sqaure

``namespace Game1 {

public class Rectangle {

    Texture2D quadl;
    GraphicsDeviceManager graphics;
    

    public Rectangle() {

        graphics = new GraphicsDeviceManager(this);
       \\ this underlined saying cannot convert from game1.rectangle to microsoft.xna.framework.game

    }

    public void size() {

        quadl = new Texture2D(this.GraphicsDevice, 100,100);
        \\ GraphicsDevice underlined
    }    
}

``}

I then call that in my game1 class in the draw method.

However mentioned in the comments there are some errors

Thanks in advance

Hmmm. It’s because the constructor of the GraphicsDeviceManager wants a Game object as parameter, not your Rectangle…
I’d pass the GraphicsDevice in the Rectangle constructor.
Be careful since when you’re generating Texture2D objects like that (not loading textures via ContentManager), you have to manually call .Dispose() on them to get them garbage-collected…
If you’d like to draw a rectangle, take a look at this https://github.com/craftworkgames/MonoGame.Extended/blob/d178de220a7e01a4d29617424f3d1eb18a2e9b5e/Source/MonoGame.Extended/ShapeExtensions.cs in the MonoGame.Extended repo.

Sorry I’m gonna need a bit more help on it.
I understood that it want’s a game object as a parameter.

I’m just not sure what you mean like this. An example would be great thanks.

In your game class just create the GraphicsDeviceManager there as a public variable then call it from your rectangle.

You can even set your rectangle GraphicsDeviceManager = Game1.GraphicsDeviceManager if you need to keep track of it locally for some reason. No need to initialize one from scratch within the rectangle.

GraphicsDevice is the problem. You cannot do this.GraphicsDevice to produce a 2D texture in the rectangle class

No, in your game class in the constructor (Usually by default it should have a variation of this)

graphicsDeviceManager = new GraphicsDeviceManager(this); // either this or GraphicsDevice i cant remember

Then if that graphicsDeviceManager variable is public (remember this is still in your game class)

from the rectangle you can do quadl = new Texture2D(Game1.graphicsDeviceManager, 100,100);

If your Game1 class is private (as is default when makign a new project) you can just put GraphicsDeviceManager gdm in the size parameter so it requires passing it in or make the Game1 a global variable (Though the parameter one is recommended since are faster performance than global variables)

Ok.
In my Game1 class

public GraphicsDeviceManager graphics;

Game1 constructor

graphics = new GraphicsDeviceManager(this);

Rectangle class

quadl = new Texture2D(Game1.graphics, 100,100);

The Game1.graphics is underlined saying an object reference is required for a non static field, method or porperty

hmm, yeah like i mentioned earlier you can go with adding the GraphcisDeviceManager to the parameter and passing that in, idk where the static object is that could be causing that issue but the parameter would fix it without requiring changing what is and isnt static.

Do you mean something like this?

Game1 class

Rectangle rt = new Rectangle(graphics);

Rectangle class constructor

class Rectangle{
    public Rectangle(GraphicsDeviceManager graphics){
    }
}

Yeah, so ur rectangle class can look like:

public class Rectangle {
Texture2D quadl;
GraphicsDeviceManager graphics;

public Rectangle(GraphicsDeviceManager gdm) { graphics = gdm; }

public void size() {
    quadl = new Texture2D(this.graphics, 100,100);
}    

}

(It required at least 20 characters to respond so i figured id go the extra mile and write it out for ya :D)