3 Replies - 2764 Views - Last Post: 22 February 2010 - 06:11 AM

#1 bgonzales999   User is offline

  • D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 108
  • Joined: 07-February 09

Unassigned local variable a?

Posted 20 February 2010 - 09:47 PM

I'm getting a error in Xna about a PaintEventArgs variable. I have absolutely no idea what is wrong.

Error 1: Use of unassigned local variable 'a' C:\Users\Ben\Desktop\Tic Tac Toe\Tic Tac Toe2\Game1.cs

I believe it is this problem that leads to me getting a System.TypeLoadException error which will not let me deploy my game.

protected void Win()
        {
            PaintEventArgs a;

            if ((ax && bx && cx) || (ay && by && cy))
            {
                Pen myPen = new Pen(System.Drawing.Color.Black);
                myPen.Width = 5;
                a.Graphics.DrawLine(myPen, 115, 45, 355, 45);
                if (piece == 'X')
                    gamesWonX += 1;
                if (piece == 'O')
                    gamesWonO += 1;
            }
         }



Is This A Good Question/Topic? 0
  • +

Replies To: Unassigned local variable a?

#2 Martyr2   User is offline

  • Programming Theoretician
  • member icon

Reputation: 5259
  • View blog
  • Posts: 14,074
  • Joined: 18-April 07

Re: Unassigned local variable a?

Posted 20 February 2010 - 10:30 PM

You defined a variable called "a" as being of type PaintEventArgs, but you haven't actually assigned an object to it. To do that you have to use the "new" keyword. You must define the variable and then create the object to associate it to the variable.

I am not sure what you are trying to do here because PaintEventArgs objects are typically created and passed to Paint events. They define parameters for the paint event about the graphics object to use as well as the location and size of where to paint.

Look up the Paint event for more information on how it is used. :)

This post has been edited by Martyr2: 20 February 2010 - 10:31 PM

Was This Post Helpful? 0
  • +
  • -

#3 bgonzales999   User is offline

  • D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 108
  • Joined: 07-February 09

Re: Unassigned local variable a?

Posted 21 February 2010 - 09:06 AM

View PostMartyr2, on 20 February 2010 - 09:30 PM, said:

You defined a variable called "a" as being of type PaintEventArgs, but you haven't actually assigned an object to it. To do that you have to use the "new" keyword. You must define the variable and then create the object to associate it to the variable.

I am not sure what you are trying to do here because PaintEventArgs objects are typically created and passed to Paint events. They define parameters for the paint event about the graphics object to use as well as the location and size of where to paint.

Look up the Paint event for more information on how it is used. :)


Ok, I got rid of the error but I am still getting a System.TypeLoadException error at runtime.

An unhandled exception of type 'System.TypeLoadException' occurred in Tic Tac Toe2.exe

Additional information: File or assembly name 'System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A', or one of its dependencies, was not found.

The error also highlights the Win() in Draw()

I have made sure that System.Drawing has references under references, content, and a using statement

This is what I did to get rid of the original error.

protected void Win()
        {
            System.Drawing.Rectangle r = new System.Drawing.Rectangle(0, 0, 480, 272);
            PaintEventArgs a = new PaintEventArgs(null, r);

            if ((ax && bx && cx) || (ay && by && cy))
            {
                Pen myPen = new Pen(System.Drawing.Color.Black);
                myPen.Width = 5;
                a.Graphics.DrawLine(myPen, 115, 45, 355, 45);
                if (piece == 'X')
                    gamesWonX += 1;
                if (piece == 'O')
                    gamesWonO += 1;
            }
         }


Finally, I created a PaintEventArgs object because Graphics.Drawline() wouldn't work without it.

Thanks for the help. :)
Was This Post Helpful? 0
  • +
  • -

#4 SixOfEleven   User is offline

  • Planeswalker
  • member icon

Reputation: 1055
  • View blog
  • Posts: 6,643
  • Joined: 18-October 08

Re: Unassigned local variable a?

Posted 22 February 2010 - 06:11 AM

If this is an XNA game, you shouldn't be trying to use the System.Graphics namespace. You would be much better to use what XNA has to offer instead. For one thing, GDI+ is much slower than XNA because XNA uses DirectX and it renders much faster. You would do much better to create graphics in some graphics program, probably not MS Paint because it does not support transparency. You will then load these graphics as Texture2Ds and draw them using the SpriteBatch class. If you really need to use primatives, there are classes for doing that as well through XNA.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1