Thomas Aylesworth

Anyone know why the compiler produces an error when the following code is compiled in an Xb0x360 project but not when it is compiled on a Windows project

public void GetTileRect(int tileX, int tileY, out Rectangle tileRect)

{

Rectangle myRect;

myRect.X = tileX * tileWidth;

myRect.Y = tileY * tileHeight;

myRect.Width = tileWidth;

myRect.Height = tileHeight;

tileRect = myRect;

}

I get a "Use of unassigned local variable 'myRect'" when assigning it to tileRect.

It's not a big deal, I can get around it by simply using "myRect = new Rectangle()". I'm just wondering why it works in one case but not the other.



Re: XNA Game Studio Express "Use of unassigned local variable" error on Xbox360 but not Windows

Joel Martinez

you know, I came across this very thing. I didn't really give it a lot of thought except for thinking that it was odd. My guess is that the desktop compiler just code-gens the call to the default constructor, while the 360 compiler doesn't --thus forcing you to put it in manually.





Re: XNA Game Studio Express "Use of unassigned local variable" error on Xbox360 but not Windows

ProfEclipse

I wonder if, perhaps, Rectangle is defined as a struct in the Windows assembly and a class in the 360 assembly.

As a follow-up question, why not avoid the whole issue (in this case) and assign directly to tileRect in your function That way you avoid the copy from myRect to tileRect.





Re: XNA Game Studio Express "Use of unassigned local variable" error on Xbox360 but not Windows

thomas.aylesworth

ProfEclipse wrote:

I wonder if, perhaps, Rectangle is defined as a struct in the Windows assembly and a class in the 360 assembly.

I hope not, since that would have implications for garbage collection. Shawn and others have said that Rectangle is a value type.

ProfEclipse wrote:

As a follow-up question, why not avoid the whole issue (in this case) and assign directly to tileRect in your function That way you avoid the copy from myRect to tileRect.

That was the way I had it originally, and it gives the same error, complaining that out parameter tileRect is unassigned. The version I posted was just a sample to demonstrate the error.





Re: XNA Game Studio Express "Use of unassigned local variable" error on Xbox360 but not Windows

Joel Martinez

the reason is because you're using the out keyword. And the rules state that an out variable must be assigned to. On windows, the compiler is obviously smart enough to know that the constructor call is implicit





Re: XNA Game Studio Express "Use of unassigned local variable" error on Xbox360 but not Windows

Stephen Styrchak - MSFT

The same compiler is used for Xbox 360 and Windows projects.

Can you verify that the Rectangle type being resolved is the one you expect Put the mouse over "Rectangle" in the editor and the tooltip should give you the fully-qualified name. Then try the same thing in your other platform's project. If you have different using statements in each project, they may be different. This should also show you whether it is a struct or a class.

If they are both resolving to "struct Microsoft.Xna.Framework.Math.Rectangle", then this is indeed a strange bug. If they are resolving to different Rectangle types, then it's possible you have different references and/or using statements between your projects.

--Stephen





Re: XNA Game Studio Express "Use of unassigned local variable" error on Xbox360 but not Windows

thomas.aylesworth

Stephen Styrchak - MSFT wrote:

Can you verify that the Rectangle type being resolved is the one you expect Put the mouse over "Rectangle" in the editor and the tooltip should give you the fully-qualified name. Then try the same thing in your other platform's project. If you have different using statements in each project, they may be different. This should also show you whether it is a struct or a class.

Intellisense shows it resolving to the correct fully qualified name in both projects: "struct Microsoft.Xna.Framework.Rectangle".

If they are both resolving to "struct Microsoft.Xna.Framework.Math.Rectangle", then this is indeed a strange bug. If they are resolving to different Rectangle types, then it's possible you have different references and/or using statements between your projects.

On a whim, I decided to make sure it wasn't something else strange about my environment, so I created a new Xbox-only project from scratch, selecting the "Xbox 360 Game" template for the project. I then added the following code to the constructor:

public Game1()

{

graphics = new GraphicsDeviceManager(this);

content = new ContentManager(Services);

Rectangle rect1;

Rectangle rect2;

rect2.X = 10;

rect2.Y = 10;

rect2.Width = 100;

rect2.Height = 50;

rect1 = rect2;

}

As I expected, I got the same error: "Use of unassigned local variable 'rect2'."

I then created a new Windows Game project and added the same code above to its constructor. It compiled and ran cleanly.





Re: XNA Game Studio Express "Use of unassigned local variable" error on Xbox360 but not Windows

Jon Watte

Perhaps the compiler warning settings are different in the two project templates Have you compared the settings, especially in the advanced mode






Re: XNA Game Studio Express "Use of unassigned local variable" error on Xbox360 but not Windows

Leaf.

I have seen this problem too. It's related to the Rectangle type that is defined in the Xbox 360 assembly. I tested by compiling the same source with the same command line apart from the referenced assembly.

Source:
using Microsoft.Xna.Framework;
static class Program
{
static public void Main()
{
Rectangle rect1;
Rectangle rect2;

rect2.X = 10;
rect2.Y = 10;
rect2.Width = 100;
rect2.Height = 50;

rect1 = rect2;
}
}

Windows cmd line:
csc.exe /r:"C:\Program Files\Microsoft XNA\XNA Game Studio Express\v1.0\References\Windows\x86\Microsoft.Xna.Framework.dll" /target:exe test.cs

Xbox 360 cmd line:
csc.exe /r:"C:\Program Files\Microsoft XNA\XNA Game Studio Express\v1.0\References\Xbox360\Microsoft.Xna.Framework.dll" /target:exe test.cs

A little investigation of the assemblies shows that the Xbox 360 version of Rectangle looks to have an extra private field member. That field member is not being initialized by the above code and so the compiler correctly complains. The same also appears to be true of some other structs such as Vector2, Vector3, and Vector4 - each has an extra Int32 field.

Having those structs be different sizes on the Xbox 360 would have caused lots of code to fail so that can't be the case. Checking the size of Rectangle using this line of code shows that Rectangle is the expected size of 16 bytes on both platforms.

System.Runtime.InteropServices.Marshal.SizeOf(typeof(Rectangle));

The Xbox 360 assemblies supplied with the XNA framework are, I guess, just standins. They have no actual code, just empty types so that you can reference the assemblies when compiling on Windows. The real assemblies are stored on the Xbox 360.

Unfortunately in the case of Rectangle and some other structs the standin assemblies have slightly different types to the Windows assemblies and the real Xbox 360 assemblies. I hope this is something that can be fixed for the next release.

Cheers,
Leaf.






Re: XNA Game Studio Express "Use of unassigned local variable" error on Xbox360 but not Windows

thomas.aylesworth

Leaf. wrote:

Unfortunately in the case of Rectangle and some other structs the standin assemblies have slightly different types to the Windows assemblies and the real Xbox 360 assemblies. I hope this is something that can be fixed for the next release.

Leaf, thanks for the extra investigative work. That should be an easy problem for them to fix. In the meantime, it's easy enough to work around.

Tom