June 2006 Blog Posts
I've been doing a "bit" of .net development lately and i had a problem where i would get
a variable of type object and it's type in a string. So I wondered how to convert it to proper type at runtime.
The solutions is one of those things that is unbeliveably hard to find because you don't really know what
to search for, but when you find it, it seems unbeliveably simple. :)
Here it is:
string sType = "System.Int32";
object o1 = "123";
object o2 = Convert.ChangeType(o1, Type.GetType(sType));
Type t = o2.GetType(); // this returns Int32 Type
I often wondered why is there no ISortable interface of somekind in .Net.
I recently needed to sort items in the context menu in alphabetical order.
Sorting them was the begining of a journey which produced this class.
It sorts simple objects like int, string, etc... as well as complex objects
that have properties you wish to sort on.
EDIT: 2006-07-06
I've removed the code because I published version 2 of this class which is better and can be found here.
I had to convert a project from .Net 1.1 to .Net 2.0 this week.
It built ok with a few warnings. Amongst others there was this one:
Warning: 'System.Threading.WaitHandle.Handle' is obsolete: 'Use the SafeWaitHandle property instead.'
That's cool, no problem, right? As it turns out it's not just replacing one property with another.
Handle property is IntPtr type and SafeWaitHandle is a class.
So .Net 2.0 has a new class called SafeWaitHandle which represents a wrapper class for a wait handle.
Why it's there is well explained on BCLTeam's blogs here.
So what to do?
It preety simple in the end.
instead of
_Event.Handle;
just use
_Event.SafeWaitHandle.DangerousGetHandle();
where _Event is...
If there was one thing that was going preety steadily on my nerves in VS2005 was region collapsing/opening with mouse.
So i was determined to find a shortcut for it.
And I found so much more...
Every shortcut there is in Visual Studio 2005 is listed here.
All SSMS keybard shortcuts are listed here.
These two pages are the holy grail of fast development if you ask me :))
oh and by the way.... collapsing/opening a region is CTRL + M + M. Yes, hit M twice while holding CTRL.
And do try CTRL + I... it's pure pleasure. a hint: Incremental Search :) <-- select text...
I've stumbled accross this post today that explains the Rozenshtein method of pivoting data.
It's quite interesting. I haven't seen the SIGN() function being used at all in SQL server in practice yet so this is kind of cool if you ask me :)
I ran into this little gem today while doing some deletes and forgeting Delete syntax :)))
There is now Output clause for DELETE, INSERT and UPDATE statements.
It outputs the affected rows into a table variable, a simple resultset or into a table.
I guess auditing is possible this way too now.
Example:
create table test(id int identity(1,1), name varchar(100))
insert into test (name)
select 'name 1' union all select 'name 2' union all select 'name 3' union all
select 'name 4' union all select 'name 5' union all select 'name 6' union all
select 'name 7' union all select 'name 8' union all...
Here's an interesting way of contencating values in one column. It makes use of the new XML capabilities.
use master
select column_name as col1 FROM INFORMATION_SCHEMA.Columns
WHERE table_name = 'spt_values'
select (select column_name as col1 FROM INFORMATION_SCHEMA.Columns
WHERE table_name = 'spt_values' for xml raw, elements, type).query('
for $col1 in (row/col1)
...