Posts in category .net - page 22

Yesterday I predicted the ability to run XNA code on our Xbox 360’s today.

We’re not quite there yet but we are a lot closer than most people believed this time yesterday.

Contrary to preliminary info on beta 2 of XNA Game Studio before it release it DOES in fact contains the necessary pieces for 360 development.

Specifically the registration window inside XNA Game Studio, shown here, the XnaTrans.exe tool for transferring builds over to the 360 and a surprise XNA Remote Performance Monitor for Xbox 360 tool.

I believe we have all the software we need right now. All we need Microsoft to do is to enable the Xbox 360 side – most likely a menu option alongside the Connect to media PC that’ll throw the connection key up on the screen that the PC side needs.

If anyone from the XNA team wants to turn mine on for testing my gamertag is DamienG ;-)

Update

It would seem the XNA team are prohibited from allowing beta code to run on retail Xbox 360’s :( Let’s hope they get to a 1.0 release sooner rather than later. A comprehensive look at the other beta 2 changes can be found at the XNA Team Blog.

Adding text to a text box

People have asked (in the IRC #CSharp) why adding text to a TextBox is so slow and flickery. Normally they are trying:

textBox1.Text+=myNewText;

The problem with this is that it copies all the Text from the text box then adds myNewText to it and copies the whole result back. This is because strings in .NET are immutable, i.e. can’t be changed, and so adding one string to another always results in this overhead (and hence the existence of the StringBuilder class).

The solution is to abandon the slow, inefficient string concatenation and use the method AppendText thusly:

textBox1.AppendText(myNewText);

Which is fast and efficient whilst also being available to TextBox, RichTextBox and MaskedTextBox (by virtue of being a method of BaseTextBox).

They usually also ask how to make the text box scroll to the end. Just use the following line:

textBox1.ScrollToCaret();

Don’t forget keyboard input

Check those tab orders and accelerator keys!

Creating dynamic controls

If you’re ever unsure how to work with a dynamic control just create it in Visual Studio’s Designer and then head into the .designer.cs file and examine the code it generates.

Windows Forms FAQ

There’s a possibility that tomorrow will see the announcement of XNA Game Studio Beta 2 with support for compiling and running applications on your Xbox 360.

Originally this was scheduled for the final 1.0 release and would involve a $99 annual fee for the privilege but what better way to get hype and excitement than to offer it free for a month or two while it’s being polished and tested during the beta phase?

Before you think I’m dreaming consider that Microsoft released the Xbox 360 Halloween firmware update this morning complete with XNA support, admittedly overshadowed by the announcement of 1080p support – another thorn in Sony’s leaky side.

The official release notes made it clear that the XNA support was tied into future availability and subject to subscriptions. The page on Xbox.com just comes out and plain says “now”.

I’m a fan of parameterized queries with a strong dislike for building SQL (or other magic strings). Encoding, escaping errors, localization formatting problems and injection can run rampant when you think everything is a string.

Problem

Today I found myself a quandary as I needed to SELECT records based on a list of values I had. e.g.

SELECT*FROMProductsWHEREProductCodeIN('ABC123','DEF456','GHI789')

At first glance the relevant parameterized version might look like:

SELECT*FROMProductsWHEREProductCodeIN(@productlist)

The problem here however is that if you put a comma-separate list of items into a string parameter named @productlist then it sends this to the database server:

SELECT*FROMProductsWHEREProductCodeIN('ABC123, DEF456, GHI789')

That’s not what we want at all.

This hack isn’t pretty and it has some limitations:

Only works with named parameters

Could upset some DB providers that don’t like having command parameters removed or command text modified

Parameter name to replace must be totally unique – i.e. not exist as a subset of another parameter name

Only preserves the basic IDbCommand declared properties

It should however work across DB providers and types.

Usage

The previous example would mean we use exactly the expected parameterized version with @productlist in place.

Add the parameter as you’d expect but instead of assigning a string/numeric to it assign something IEnumerable.

Finally call this method against the command and parameter before you execute it for the ‘magic’ to happen: