Christian Vucossa programming blog

game programming, daily troubles and solutions

Archive for the ‘exception solving’ Category

all about fixing of programming exceptions

The Evil NHibernate’s NonUniqueObjectException !

Posted by Christian on November 4, 2009

Today I faced this ancient evil… and it almost destroyed me.

This exception comes out when you try to save or update an object with the same id of another object that is already in the scope of  the NHibernate session.

My scenario was this: I had to save the instance of an object. Well, I have to choose if use a Insert or an Update. To do so, I query the db in search of an object with the id of mine and act accordingly.

BUT, the query keeps in the session the instance of the object and the next Update operation fails with the NonUniqueObjectException. What to do? Well, in this case you have to send in update the same object the GetByID returns to you.

So:

1) Query the db and keep the instance of the object A it returns (if any)

2) Update A with the data you store in the original updated object B

3) Save A instead of directly save B

and you’ll defeat the Evil NonUniqueObjectException!

Posted in exception solving | Leave a Comment »

Visual Studio .NET 2003 lib path limit

Posted by Christian on May 22, 2009

Yes, it is. The include lib path in Visual Studio .NET 2003 (C++) has a lenght limit, something about 2k (I haven’t measured precisely). So, if you run into the ugly LNK1104 fatal error and there is nothing wrong on your settings, maybe you have a too long lib list.

Now, far from me the will to debate if this is a silly limitation, after all one could say that a more than 2k lenght lib path list is far more silly too.

Posted in exception solving | Tagged: | Leave a Comment »

DataGridView Cell’s editing: handle user’s input

Posted by Christian on January 27, 2009

Today I faced an issue that stole me a couple of hours of lazy work… My need was to intercept all the ‘.’ chars inputed by the user on a DataGridView cell and replace them with commas. With my big surprise, I have not found any suitable event of method.

After a coffebreak, I returned with clear mind and restored energies at the problem and I found this event on the DataGridView object:

EditingControlShowing

Neither the name nor the help tooltip of the intellisense help me so much understanding its behavior, but I registered to it and took a try.

Its DataGridViewEditingControlShowingEventArgs provide me with a Control object that is exactly the control used by the cell to handle input, a TextBox in my case. And now the magic.

I tried to treat the control as a normal TextBox, registering to its KeyPress event and it works!

myGrid.EditingControlShowing+=new DataGridViewEditingControlShowingEventHandler(myGrid_EditingControlShowing);

private void myGrid_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e) {
if (e.Control is TextBox) {
((TextBox)e.Control).KeyPress +=new KeyPressEventHandler(HandleKeyPress);
}
}

private void HandleKeyPress(object sender, KeyPressEventArgs e) {
// My stuff here…
}

Another day at work, another .NET  trick learned…

Posted in exception solving, various | Tagged: , | Leave a Comment »

The float to int conversion’s big mystery

Posted by Christian on December 31, 2008

Someone must explain me this thing… I was working on my roguelike, namely the wall collision, when I came across one of the strangest things seen recently. My issue was to convert space coordinate of my @ into 2d matrix coordinate of the map, a bi-dimensional array. My space2map function perform some calculation and give me a x,y values, quite straight.

It seemed to work, but after a bit of testing things began to go wrong… when I was expecting a 2 I get a 1 instead, or the like. For some debugging hours I was sure I was wrong, but in the end I found myself in front of a thing like this:

(0.2 + 0.3) / 0.5 = 0

It was enough to make me think that yes, math is an opinion. A compiler opinion, at least.

After a while google gave me the answer: there is something on the float to int conversion. A really magic function came with the answer, this:

int iround(float x)
{
return (int)floor(x + 0.5);
}

where floor() is the math.h C function.

I tried with this and it works. Then, I wrote a simple program to test this mystery, and this is the result:

The float to int test

The float to int test

For the record, this happens on GCC for Ubuntu 8.04.

Posted in exception solving | Tagged: | Leave a Comment »

Windows Mobile and the “unknown publisher” exception

Posted by Christian on December 9, 2008

A few days ago I was deploying a new version of a software of mine on a PDA running Windows Mobile 6.0 and I have had to deal with a boring “unknown publisher” prompt. My new compiled dll was not recognized as trusted. What to do to get rid of this nuisance?

Well, I found this helpful link where the guy explains how to do but lacks of some important details so that I was obliged to surf again to fix. At the end of the story, I found a way.

To get rid of the message we have to edit a registry key. My app was in .NET (C#), and here is the code that fix the problem:

string keyname = “0000101a”;
RegistryKey securityKey = Registry.LocalMachine.OpenSubKey(“Security”)
.OpenSubKey(“Policies”).OpenSubKey(“Policies”, true);
object val = securityKey.GetValue(keyname);
securityKey.SetValue(keyname, 1, RegistryValueKind.DWord);

Hope this helps.

Posted in exception solving | Tagged: , | Leave a Comment »

 
Follow

Get every new post delivered to your Inbox.