Visual Studio

Use regular expressions in Visual Studio to clean up code

Friday, February 20th, 2009 | .NET | No Comments

This is a reminder to myself.

Knut, remember that Regular Expressions are very handy to clean up a lot of messy code.

Here is an example:
The following function call results in a code analysis warning:

DBHelper.SetPropertyFromDB(m_City, dr("CITY"))

It's a helper function that returns the typed value from a DBValue. Not really useful these days with NHibernate or Typed Datasets, but it's code from an old application. Here is the signature of the code:

Shared Sub SetPropertyFromDB(ByRef pProperty As Object, ByVal value As Object)

The warning reported is:

"Warning: Implicit conversion from 'Object' to 'String' in copying the value of 'ByRef' parameter 'pProperty' back to the matching argument."

To fix this, I created a new method that returns the correct typed value instead of returning the referenced parameter and marked the old method "Obsolete":

Public Shared Function GetDBValue(ByVal pProperty As Object, ByVal dbValue As Object) As Object

Now, I had to rewrite all the calls to the function (several hundred calls). This included returning the typed value into the same variable as the first parameter in the function call, like this:

m_City = DBHelper.GetDBValue(m_City, dr("CITY"))

This is easily achieved by using Regular Expressions in the "Find and Replace" dialog in Visual Studio:

Find What:

DBHelper.SetPropertyFromDB\({.*},

(note: the \ escapes the ( and the expression group is marked by a {})

Replace with:

\1 = DBHelper.GetDBValue(\1,

(note: the \1 will represent the expression group)

Pretty simple, and very powerful :-)

Tags: ,

bug: VS 2008 – Web Application Project opened as Web Site

Monday, November 3rd, 2008 | .NET | 6 Comments

I just bumped into a weird Visual Studio (aka. Vicious Studio)  bug. Not the first, but this is a pretty annoying one. I have taken an old .NET 1.1 Web application and converted it to .NET 3.5 using the VS Conversion Wizard. Everything seems ok, and I convert to Web Site to a Web Application Project. No problems so far. I fix some issues, close the solution, do some other stuff. Some days later I open the solution again. I then have about 200 errors and the Web Application Project is opened as a Web Site. Also the designer and resx files are not connected to the asp and codebehind files:

It's not possible to change the project type in Visual Studio, as the project does not have a "properties" context menu item. The only option in the context menu is "property pages" as with regular web sites. Visual Studio actually thinks it's a Web Site and not a Web Application project anymore. The other missing context menu option is the "Convert to Web Application". So Visual Studio thinks it's a Web Site that cannot be converted to a Web Application Project. Not surprisingly as I converted the Web Site to a Web Application Project right after the Conversion Wizard.

Ok, I have to start digging into the project and solution files. Comparing them to other solutions using Web Application project files, I can't seem to find anything suspicious. After some googling with no result, I decide to stare at the screen for a while and use WinMerge to determine what the difference might be. Suddenly I find one minor difference:

The solution file that works:

Project("{F184B08F-C81C-45F6-A57F-5ABD9991F28F}") = "Clock", "Clock.vbproj", "{3B3D0F02-D310-4BFB-83AD-F62758BB8624}"

The one that fails:

Project("{F184B08F-C81C-45F6-A57F-5ABD9991F28F}") = "Calendar", "Calendar\", "{7B411329-B1CB-457F-A954-898DX16B85A6}"

That's it. The Calendar project reference is missing the whole path to the project file. When i change it to:

Project("{F184B08F-C81C-45F6-A57F-5ABD9991F28F}") = "Calendar", "Calendar\Calendar.vbproj", "{7B411329-B1CB-457F-A954-898DX16B85A6}"

it opens as a Web Application Project, and now the resx and designer files are properly connected:

There might be other issues causing this behaviour, but at least this solved my problem. This is just the everyday life using Visual Studio. Actually I think it's a great IDE, but there are some strange behaviours now and then.
Update - In some occasions you might wan't to remove the .webinfo file. This file can be causing this behaviour.  Just remove it and instead set the startup path for your application in properties for the Web Application project.

Tags: ,

Search

Knut Hamang

This is the official site for Knut Hamang. I am a professional technical consultant specialized in Microsoft and .NET technologies and platforms. I work as an independent consultant in Norway and have more than 10 years of professional experience. To get in contact with me, please send me an email or check my public profile on View Knut Hamang's profile on LinkedIn