Visual Studio
Use regular expressions in Visual Studio to clean up code
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
bug: VS 2008 – Web Application Project opened as Web Site
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:
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:
Search
Knut Hamang
Recent Posts
Recent Comments
- Lori on Html to Pdf in .NET
- Susan on Html to Pdf in .NET
- Susan on Html to Pdf in .NET
- Ananth on Html to Pdf in .NET
- Tim M on Passing an object to ObjectDataSource

