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 8183643
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T01:13:33+00:00 2026-06-07T01:13:33+00:00

I’m building a game using C# in XNA and I’m not sure which of

  • 0

I’m building a game using C# in XNA and I’m not sure which of these four ways is best to refer to the “Viewport” for reading access only:

  1. Have a public static variable that every class can access by calling Game.Viewport (Game is the main class:

    public static readonly Viewport Viewport = GraphicsDevice.Viewport;

in use:

rectangle = new Rectangle (Game.Viewport/2, ...
  1. Have a local variable that refers to the Viewport for each class that needs the viewport:

    private readonly Viewport viewport = GraphicsDevice.Viewport;

in use:

rectangle = new Rectangle (viewport/2, ...
  1. Use a local variable passed down into the constructor of each class that needs the viewport (this would be the only one that didn’t require imported Microsoft.Xna.Framework.Graphics):

    private readonly Viewport viewport;

then in the constructor:

viewport = pViewport;

in use:

rectangle = new Rectangle (viewport/2, ...
  1. Directly reference the viewport from the class library everywhere where it’s needed (in use):

    rectangle = new Rectangle (GraphicsDevice.Viewport.Width/2, …

I wondering which one is best in terms of speed and which one is best in terms of readability. Personally, I feel like the fourth method would be easiest and fastest so I don’t need to create any references to the Viewport and it’s clear that I’m referring to something that is global. It seems like it’s taking out the “middleman”, but then you have to import the XNA graphics library to every class that uses the Viewport.

What do you guys think?

  • 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-07T01:13:35+00:00Added an answer on June 7, 2026 at 1:13 am

    I suppose it depends on your design. If this is just a simple, one-off application, I’d go with your first example. You have a single viewport, none of your classes are dependent on the GraphicsDevice or however you decide to manage that viewport.

    Storing the single object there and referencing it in your code will be fast; it’s just a field reference so they don’t come much faster than that. Calling GraphicsDevice.Viewport every time, especially since it’s a property means you’ll be running a property getter method every single time you want to access it.

    EDIT: If you want to quickly throw in a property wrapper on Game.Viewport (instead of a field) so you can track access (say for logging) temporarily, then you’re free to do it this way. If you choose the latter with GraphicsDevice.Viewport, you have to do it every place in your code where you call it (code duplication).

    If this is for a more reusable library, if it makes sense to restrict access to your Viewport to only the relevant classes, or want to separate your dependency to Game, passing it down via constructors as dependency injection can be worthwhile, especially when it comes to unit testing.

    I wouldn’t even consider option 2 (evaluating GraphicsDevice.Viewport for each class as a private field). At the very least you could wrap it with your own reusable static class (which is what you’re doing with Game)

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

Sidebar

Related Questions

I am reading a book about Javascript and jQuery and using one of the
We're building an app, our first using Rails 3, and we're having to build
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this

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.