Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8104471
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T23:51:13+00:00 2026-06-05T23:51:13+00:00

There I am making a 2D game in C# XNA 4.0, and run across

  • 0

There I am making a 2D game in C# XNA 4.0, and run across yet again a petty annoyance of mine; the Rectangle. For those using basic collision, this is almost a necessity. For almost any game object created you need to have a rectangle. Then I go to change the X, check collision, or anything else. And there I begin the never-ending battle of objectName.Rectangle.Whatever. To get around this I of course give the class of objectName properties/methods that access these for me.

Then I dared to dream. I had grand designs to make a basic game object class that everything drawable would inherit from, that would allow parenting, precise local coordinates (floats), and hold the texture/spritebatch. To make this complete I was ready to inherit from Rectangle, to use all the methods and attributes that it holds. Heck, whenever something required a Rectangle, I could be so lazy as to say objectName, not objectName.Rectangle.

Then I realized, not a chance. I started out depressed as my oh-so-clever idea was smashed to bits. Since then my perfect little class holds a rectangle, with various methods and properties accessing it as needed. I have also taken the chance to have it inherit from the XNA DrawableGameComponent. While in the long run this has been more practical, every time I view a draw or update method and see the call to rectangle I often wonder, was there ever a hope to do what I had wanted? Was there some clever work around that I could have done? Or was inheriting from a Rectangle truly sealed from my grasp?

While using the DrawableGameComponent class provided in XNA allows most game-object related actions happen inside the classes Update() method, every time outside of a class I need to reference not to property of a Rectangle, but rather the Rectangle itself, I am slightly peeved considering that in really every way my object is, in fact, and souped-up Rectangle. And then once again I can find myself asking:

Is there any way to inherit from a pre-defined struct, or give the project the impression you are (a work-around)?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T23:51:15+00:00Added an answer on June 5, 2026 at 11:51 pm

    Inherit no, but you can add a lot of ‘default’ functionality to the Rectangle object with extension methods.
    For example

      //(inside a static class)
      public static int GetSurface(this Rectangle rect){return rect.Width * rect.Height;}
    
      //calling
      Rectangle rect;
      var s = rect.GetSurface();
    

    That said, what I normally do is encapsulate said struct. Use that class as the base object, and add an operator so that it can be implicitly cast to a Rectangle. That way it can be passed to a method that needs a rectangle without casting.

        public class MyRect //class so you can inherit from it, but you could make your own struct as well
        {
            public int X { get; set; }
            public int Y { get; set; }
            public int Width { get; set; }
            public int Height { get; set; }
            public int Right { get { return X + Width; } }
            public int Bottom{ get { return Y + Height; } }
    
            public static implicit operator Rectangle(MyRect rect)
            {
                return new Rectangle(rect.X, rect.Y, rect.Width, rect.Height);
            }
    
            public static implicit operator MyRect(Rectangle rect)
            {
                return new MyRect { X = rect.X, Y = rect.Y, Width = rect.Width, Height = rect.Height };
            }
    
        }       
     }
    

    Now you can create your own rect manually or from an existing one:

      MyRect rect = ARectangleVar
    

    And you can use it in legacy methods that expect a Rectangle without casting

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm making a 3D game in XNA using Ox Engine. There is very little
I am making a game using C# with the XNA framework. The player is
I'm making a game using XNA framework, so I use a lot functions that
I am currently making a golf ball game using C# and XNA. So far
I'm making an xna game and there is going to be many classes that
I'm using node.js and socket.io, and I'm making a game. There are quite a
I'm making a multiplayer game in XNA 4.0, using the .NET socket classes. Since
I'm making a simple 2D game for Windows Phone 7 (Mango) using the XNA
I am making an HTML5 game engine. There is only one file that is
I'm making a small online game, where (what do you know) there'll be multiple

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.