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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T15:28:36+00:00 2026-05-19T15:28:36+00:00

I’m writing an XNA 3.1 application for a C#/XNA class I’m taking right now.

  • 0

I’m writing an XNA 3.1 application for a C#/XNA class I’m taking right now. It’s a very simple introductory assignment where the user simply changes the color of the screen, pressing the R, G, or B buttons on the keyboard to choose a color channel and then the up and down arrows to increase or decrease the value of that channel.

If I increment a byte past 255 it wraps back to 0, and vice versa for decrementing, presumably because C# actually converts a byte to an int when doing arithmetic on them. However, my professor specifically asks that once a channel reaches its maximum or minimum value that it stays there if the user tries to go out of these bounds. I figured out a way to fix this by myself, but I didn’t find anything when I searched for solutions to this kind of problem on here or on Google. This is how I’ve solved the problem here:

/// <summary>
/// This is the main type for your game
/// </summary>
public class Game1 : Microsoft.Xna.Framework.Game
{
    // ...

    Color color;

    private int Red
    {
        set { color.R = NoWrapIntToByte(value); }
        get { return (int)color.R; }
    }

    private int Green
    {
        set { color.G = NoWrapIntToByte(value); }
        get { return (int)color.G; }
    }

    private int Blue
    {
        set {color.B = NoWrapIntToByte(value); }
        get { return (int)color.B; }
    }

    /// <summary>
    /// Converts an integer to a byte but doesn't wrap.
    /// </summary>
    /// <param name="x"></param>
    /// <returns></returns>
    static byte NoWrapIntToByte(int x)
    {
        if (x < Byte.MinValue)
            return Byte.MinValue;
        else if (x > Byte.MaxValue)
            return Byte.MaxValue;
        else
            return (byte)x;
    }
}

Every time the Draw() method is called, the color stored in color is set as the color of the screen.

Just for clarity, I realize I can’t do something like

byte x = 256;    // compiler error

the compiler will complain. However, when the user presses the up or down arrows that color channel is incremented or decremented, and if the channel is decremented below 0 or incremented above 255 it will wrap. I used int as the type of the channel properties because if I’m using something like Red -= 1 or Red += 1 there are different expectations that happen if I go out of bounds. If I’m adding I would expect the resulting value to be smaller than the original value, and vice versa if I’m decrementing. I’m using int because it allows me to detect this in the property and handle accordingly.

I think my solution is okay, but I’m curious if I’m overlooking something. I can’t be the only person who has tried to solve this problem so I’m curious what an idiomatic or best-practice way to solve this problem would be. I’m not asking for answers to my homework problem since I do already have a solution that works. However, I don’t know C# that well so I’m curious if there’s a much simpler way of doing this same thing, or with less code than what I have here. I’m aware that I can make C# throw an Overflow exception, but that requires changing project settings, and it also doesn’t tell me specifically what kind.

Thanks!

  • 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-05-19T15:28:36+00:00Added an answer on May 19, 2026 at 3:28 pm

    It sounds to me that you should just be using the “Clamp” method built into the XNA framework.

    http://msdn.microsoft.com/en-us/library/microsoft.xna.framework.mathhelper.clamp.aspx

    Basically if you want a value to say within 0 and 255 then you would just write something like this.

    static byte NoWrapIntToByte(int x)
    {
        return (byte)MathHelper.Clamp(x, 0, 255);
    }
    

    That should pretty much do exactly what it sounds like you’re looking for.

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

Sidebar

Related Questions

this is what i have right now Drawing an RSS feed into the php,
I am doing a simple coin flipping experiment for class that involves flipping a
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
Seemingly simple, but I cannot find anything relevant on the web. What is the
I am writing an app with both english and french support. The app requests
I need to clean up various Word 'smart' characters in user input, including but

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.