Last days i’ve visited a couple of interesting sessions at the Developer & IT Pro Days 2007. All in all the event was pretty well organised… In the closing word of the event, Chad Z. Hower found himself quite funny when he set the title of a form in an exception throwing application to ‘Java Application’. Unlike the rest of the crowd i didn’t see the humor in it… Anyway, when his Visual Studio Orcas started choking i wet my pants too
Monthly Archives: March 2007
Developer & IT Pro Days 2007
Posted by timvw on March 29, 2007
None comments
Simulate AutoIncrement
Posted by timvw on March 24, 2007
1 comments
Earlier today someone asked the following:
I’m trying to move selected data from one table to another. The following works apart from the destination table is not incrementing the ID (I’m not using auto increment for that field).
How can I increase the value of field_1_id for each select > insert? I’m guessing SQL doesn’t loop through each SELECT match an insert correspondingly?
Here is a possible answer: (Don’t forget to wrap these queries in a transaction if your MySQL Engine supports it)
-- Some demo tables (and data) -- CREATE TABLE table1 (id INT(10), name VARCHAR(20), PRIMARY KEY(id)); -- CREATE TABLE table2 (id INT(10), name VARCHAR(20), PRIMARY KEY(id)); -- INSERT INTO table1 VALUES (1, "tim"), (2, "mike"); SET @id := (SELECT MAX(id) + 1 FROM table2); INSERT INTO table2 SELECT @id := @id + 1, table1.name FROM (SELECT @id) AS id, table1 AS table1;
Little INotifyPropertyChanged helper
Posted by timvw on March 19, 2007
2 comments
Most implementations of INotifyPropertyChanged look as following (notice that you have to make sure that the hardcoded PropertyName is spelled correctly):
class MyClass : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private int x;
public int X
{
get { return this.x; }
set
{
if (this.x != value)
{
this.x = value;
this.OnPropertyChanged("X");
}
}
}
[MethodImpl(MethodImplOptions.NoInlining)]
private void Fire(Delegate del, params object[] args)
{
if (del != null)
{
foreach (Delegate sink in del.GetInvocationList())
{
try { sink.DynamicInvoke(args); }
catch { }
}
}
}
protected virtual void OnPropertyChanged( string propertyName )
{
this.Fire( this.PropertyChanged, new PropertyChangedEventArgs( propertyName ) );
}
}
Everytime you refactor a property you also have to make sure to refactor the string with it’s name in the setter method. Here’s a helper method that makes life a little easier:
protected void OnPropertyChanged()
{
this.OnPropertyChanged(new StackTrace(false).GetFrame(1).GetMethod().Name.Substring(4));
}
This makes the implementation of a property as simple as (No more hardcoded strings to maintain):
public int X
{
get { return this.x; }
set
{
if (this.x != value)
{
this.x = value;
this.OnPropertyChanged();
}
}
}