locked
Console.SetBufferSize suddenly throwing errors. RRS feed

  • Question

  • I was just doing a random build to test something when the set buffer size method starting giving out of bounds exceptions... I have no idea what is causing it. SetWindowSize works fine. If I comment out the buffer size, everything works.

    static int width = 100;
            static int height = 35;
    
            public static void Main(string[] args)
            {
                Console.CursorVisible = false;
                Console.Title = "Game 1";
                Console.OutputEncoding = System.Text.Encoding.Unicode;
    
                 System.Console.SetBufferSize(width,height);
                 System.Console.SetWindowSize(width, height);

    Is this just a windows bug or what?


    • Edited by JeffrBaker Friday, July 27, 2012 6:04 AM
    Friday, July 27, 2012 6:01 AM

Answers

  • According to MSN, this exception is thrown when either width is less than Console.WindowLeft+Console.WindowWidth OR height is less than Console.WindowTop+Console.WindowHeight;

    So, Either you can interchange those two statements as below, 

    static int width = 100;
    static int height = 15;
    
    public static void Main(string[] args)
    {
        Console.CursorVisible = false;
        Console.Title = "Game 1";
        Console.OutputEncoding = System.Text.Encoding.Unicode;
    
        System.Console.SetWindowSize(width, height);
        System.Console.SetBufferSize(width, height);
    }

    Or add a boundary check as,

    static int width = 100;
    static int height = 35;
    
    public static void Main(string[] args)
    {
        Console.CursorVisible = false;
        Console.Title = "Game 1";
        Console.OutputEncoding = System.Text.Encoding.Unicode;
    
    
        if (Console.WindowLeft + Console.WindowWidth < width && 
            Console.WindowTop + Console.WindowHeight < height)
        {
            System.Console.SetBufferSize(width, height);
        }
        System.Console.SetWindowSize(width, height);
    }

    I hope this helps.

    Please mark this post as answer if it solved your problem. Happy Programming!

    • Marked as answer by JeffrBaker Friday, July 27, 2012 3:01 PM
    Friday, July 27, 2012 6:36 AM

All replies

  • Hi,

    remove Static key word from

    static int width = 100;
            static int height = 35;

    if your problem doesn't solve, then let us know the exception.

    Thanks

    Friday, July 27, 2012 6:26 AM
  • Hi,

    remove Static key word from

    static int width = 100;
            static int height = 35;

    if your problem doesn't solve, then let us know the exception.

    Thanks

    Friday, July 27, 2012 6:26 AM
  • According to MSN, this exception is thrown when either width is less than Console.WindowLeft+Console.WindowWidth OR height is less than Console.WindowTop+Console.WindowHeight;

    So, Either you can interchange those two statements as below, 

    static int width = 100;
    static int height = 15;
    
    public static void Main(string[] args)
    {
        Console.CursorVisible = false;
        Console.Title = "Game 1";
        Console.OutputEncoding = System.Text.Encoding.Unicode;
    
        System.Console.SetWindowSize(width, height);
        System.Console.SetBufferSize(width, height);
    }

    Or add a boundary check as,

    static int width = 100;
    static int height = 35;
    
    public static void Main(string[] args)
    {
        Console.CursorVisible = false;
        Console.Title = "Game 1";
        Console.OutputEncoding = System.Text.Encoding.Unicode;
    
    
        if (Console.WindowLeft + Console.WindowWidth < width && 
            Console.WindowTop + Console.WindowHeight < height)
        {
            System.Console.SetBufferSize(width, height);
        }
        System.Console.SetWindowSize(width, height);
    }

    I hope this helps.

    Please mark this post as answer if it solved your problem. Happy Programming!

    • Marked as answer by JeffrBaker Friday, July 27, 2012 3:01 PM
    Friday, July 27, 2012 6:36 AM
  • @Adavesh

    That worked! Thanks, I just switched the statements. I have no idea why it was working before, then suddenly started giving errors.

    Friday, July 27, 2012 3:01 PM