Search and Replace with Regular Expressions
You can use the Find-replace sequence of
"F{:c+} "m_\1
to replace the F prefix in the field name in the storage argument with an m_ prefix, and you can use the following pairing to fix the Return and assignment statements in the Property getters and setters.
:WhF{:c+} m_\1
Of course, the previous pairing will also Find "Function", "For", the property "Fax", and "firstFive". You could add the prevent match element ~(X) sub-expression to eliminate these from the search results, for example
:WhF~(unction)~(ax)~(or)~(irstFive){:c+} m_\1
and the regular expression would successfully skip "Function", "For", "Fax", and "firstFive". Here is the completely revised code after the find and replace operation using the three variations of expressions on the code (see Listing 2).
Listing 2: The same code as listing 1 with the modified field elements using a more conventional naming style.
Imports System.Data.Linq Imports System.Data.Linq.Mapping Imports System.Reflection Imports System.Text Module Module1 Sub Main() Const connectionString As String = _ "Data Source=.\SQLEXPRESS;AttachDbFilename=c:\temp\ _ northwnd.mdf;" + _ "Integrated Security=True;Connect Timeout=30; _ User Instance=True" Dim northwind As Northwind = _ New Northwind(connectionString) Dim customers As Table(Of Customer) = _ northwind.GetTable(Of Customer)() Dim firstFive = customers.Take(5) For Each item In firstFive Console.WriteLine(item) Next Console.ReadLine() End Sub End Module Public Class Northwind Inherits DataContext Public Sub New(ByVal connectionString As String) MyBase.New(connectionString) Me.Log = Console.Out End Sub End Class <Table(Name:="Customers")> _ Public Class Customer Private m_CustomerID As String Private m_CompanyName As String Private m_ContactName As String Private m_ContactTitle As String Private m_Address As String Private m_City As String Private m_Region As String Private m_PostalCode As String Private m_Country As String Private m_Phone As String Private m_Fax As String <Column(Name:="CustomerID", Storage:="m_CustomerID")> _ Public Property CustomerID() As String Get Return m_CustomerID End Get Set(ByVal value As String) m_CustomerID = value End Set End Property <Column(name:="CompanyName", Storage:="m_CompanyName")> _ Public Property CompanyName() As String Get Return m_CompanyName End Get Set(ByVal value As String) m_CompanyName = value End Set End Property <Column(name:="ContactName", Storage:="m_ContactName")> _ Public Property ContactName() As String Get Return m_ContactName End Get Set(ByVal value As String) m_ContactName = value End Set End Property <Column(name:="ContactTitle", Storage:="m_ContactTitle")> _ Public Property ContactTitle() As String Get Return m_ContactTitle End Get Set(ByVal value As String) m_ContactTitle = value End Set EndProperty <Column(name:="Address", Storage:="m_Address")> _ Public Property Address() As String Get Return m_Address End Get Set(ByVal value As String) m_Address = value End Set End Property <Column(name:="City", Storage:="m_City")> _ Public Property City() As String Get Return m_City End Get Set(ByVal value As String) m_City = value End Set EndProperty <Column(name:="Region", Storage:="m_Region")> _ Public Property Region() As String Get Return m_Region End Get Set(ByVal value AsString) m_Region = value End Set End Property <Column(name:="PostalCode", Storage:="m_PostalCode")> _ Public Property PostalCode() As String Get Return m_PostalCode End Get Set(ByVal value As String) m_PostalCode = value End Set End Property <Column(name:="Country", Storage:="m_Country")> _ Public Property Country() As String Get Return m_Country End Get Set(ByVal value As String) m_Country = value End Set End Property <Column(name:="Phone", Storage:="m_Phone")> _ Public Property Phone() As String Get Return m_Phone End Get Set(ByVal value As String) m_Phone = value End Set End Property <Column(name:="Fax", Storage:="m_Fax")> _ Public Property Fax() As String Get Return m_Fax End Get Set(ByVal value As String) m_Fax = value End Set End Property Public Overrides Function ToString() As String Dim builder As StringBuilder = New StringBuilder() builder.AppendFormat("{0}", Me.GetType().Name) builder.AppendLine() builder.AppendFormat("{0}", New String("-", 40)) builder.AppendLine() Dim info() As PropertyInfo = Me.GetType().GetProperties() For Each prop In info Try Dim value As Object = prop.GetValue(Me, Nothing) builder.AppendFormat("{0}: {1}n", prop.Name, value) Catch ex As Exception builder.AppendFormat("{0}: {1}", prop.Name, "None") End Try builder.AppendLine() Next Return builder.ToString() End Function End Class
Page 3 of 4
This article was originally published on March 17, 2008