onsdag 15 juni 2011

ASP.NET 4: Breaking changes

When migrating your ASP.NET application up to ASP.NET 4 you might get some unexpected error when you try to run your application. If you have made the migration in Visual Studio 2010, most of the problems will have already been corrected, but not all.

Read through this article on www.asp.net for a list of problems and solutions that you might (will) encounter: http://www.asp.net/learn/whitepapers/aspnet4/breaking-changes

måndag 13 juni 2011

VB.NET: Finding registered event handlers

I was wondering if there was any way in VB to see if there are any registered handlers to a specific event. I knew it could easily be done in C#, but did not know of an equivalent for VB. And after some googling, I found it. But of course, with some "quirks"..

It seems that you can only check for registered event handlers on custom events, and only by the obscure:

...
Public Event MyEvent As Action(Of T)
...
If MyEventEvent Is Nothing Then
...
So, it has to be a custom event, and you have to append 'Event' to the name of your event.. And Intellisense will not help you as it does not even allow you to type the name of your event if you have not started the line with RaiseEvent...

I can live with the fact that I have to append 'Event' to the name of my event, but why does it have to be a custom event? Why does it not work with other events such as Button.Click?

I suspect that this is because of that the field MyEventEvent might be private or protected though. So maybe you could do it via reflection. But come on.

fredag 10 juni 2011

PowerShell: Useful info about strings

Strings in PowerShell are instances of System.String (.NET). This enables us to use all functions and properties we are used to from C#/VB.NET: PS> $mystring = "Hello bob"
PS> $parts = $mystring.split(" ")
PS> [System.String]::Format("{0} {1}", $parts[0], $parts[1])
Hello bob

There are also a few string-specific operators that can be used: -split, -join, -f (scroll down to 'Special operators')

PS> write-host $("Hello {0}" -f "bob")
Hello bob
Notice the use of $(...) here, it is a sub-expression that is required in this case to make sure that the expression is executed first and sent to write-host as a single argument. See about_Operators (scroll down to 'Special operators') for more information.

Furthermore, there are special cases such as -like, -contains and -match in comparison operators that makes our lives even easier.

Concatenating strings is done in the usual way, with the plus-sign. Using variables inside a string can be done when using double-qoutes. Escaping on the other hand is by using the backtick, not backslash as most of us are used to:

PS> $myname = "bob"
PS> $mystring = "Hello " + "$myname `"The drooling monkey`""
Hello bob "The drooling monkey"

And last but not least

A few wise words about the cmdlet Select-String and a "general rule" of PowerShell:
If you're parsing a string in Windows PowerShell, you're doing something wrong
Don Jones Read the article from Don Jones here

PowerShell: Fetching the current user

To get the name of the user running the script, use: [Security.Principal.WindowsIdentity]::GetCurrent().Name

You could also use $Env:UserName and $Env:UserDomain, but these are not always what you might think at a first glance. They contain the name of the user in the active environment, which is not always the same as the user running the script.

Example (assume that the user running this line is 'foo'):

runas /user:bar /env "powershell myscript.ps1" This would run the script as 'bar' specified, but with $Env:UserName set to 'foo' because of the parameter /env

PowerShell links

Some useful links. The ones that are emphasized are, to me, a bit more important and useful.

My posts

Useful info about strings

Technet

Windows PowerShell
Basic, task-oriented info.

Windows PowerShell Owner´s manual
A good starting place

Windows PowerShell Tips
Misc tips and tricks

Windows PowerShell Core
Misc useful stuff

'About' help topics
Really useful as reference for syntax etc.

All help topics can be viewed directly from within PowerShell by typing: Get-Help about_<topic>

Listed below are a few useful ones

Windows PowerShell CmdLet Help Topics
The same goes for these, you can type: Get-Help <cmdlet>