Comments on: Parsing Columns Like A Ninja https://simpleprogrammer.com/parsing-columns-like-a-ninja/ Thu, 14 Jul 2016 18:52:32 +0000 hourly 1 https://wordpress.org/?v=7.0 By: Peter Petrov https://simpleprogrammer.com/parsing-columns-like-a-ninja/#comment-259 Thu, 01 Jul 2010 17:01:42 +0000 https://simpleprogrammer.com/?p=975#comment-259 This method has very limited usage. All the properties Name, Description, … has to be of type string. If you have different types you have to change to Action but then you can’t cast (implicitly)object to int for example. A possible solution is to use Action if you are using C# 4.0 but you loose the compile-time checking.

Just my 2 cents.

]]>
By: handcraftsman https://simpleprogrammer.com/parsing-columns-like-a-ninja/#comment-258 Thu, 01 Jul 2010 02:52:08 +0000 https://simpleprogrammer.com/?p=975#comment-258 Nice. One thing that seems fragile here is the dependency of the order of the data columns matching the order of the setters in the list. If you change the data source to no longer return the Category column you have to find and change all the property setter lists that are calling the mapping method. Consider a variation that preserves the advantages of your solution while also conforming to the Open-Closed Principle.

]]>
By: Chris https://simpleprogrammer.com/parsing-columns-like-a-ninja/#comment-257 Wed, 30 Jun 2010 19:40:14 +0000 https://simpleprogrammer.com/?p=975#comment-257 In reply to jsonmez.

I really don’t know why everyone is so hard on VB.net. It compiles into MSIL just like everything else. That aside, you’re right. That would be better for reading it from disk or if the properties were sent in the first row of the data.

]]>
By: jsonmez https://simpleprogrammer.com/parsing-columns-like-a-ninja/#comment-256 Wed, 30 Jun 2010 19:02:37 +0000 https://simpleprogrammer.com/?p=975#comment-256 In reply to Chris.

You and your VB.NET 😛

Actually, although that would work, there are a couple problems with the implementation.

1. You will be using reflection. Which isn’t necessarily bad, but it can slow things down quite a bit. 2. You will lose the type safety, because you will have to give the property name as a string.

If you mistype the property name, you could have a problem that you would only see at runtime. Also if you do a code refactor using a refactor tool, (say you change the property name), it is not likely to catch the property name in a string. You can do the same thing I did in VB.NET using VB.NET lambda syntax though: http://msdn.microsoft.com/en-us/library/bb531253.aspx

The place where I would use your solution would be if I were reading in the property names from some kind of configuration file on disk, or from a database, where I am dynamically creating the mapping.

]]>
By: Chris https://simpleprogrammer.com/parsing-columns-like-a-ninja/#comment-255 Wed, 30 Jun 2010 18:51:11 +0000 https://simpleprogrammer.com/?p=975#comment-255 How about something like this? It works well for me in VB.net; I would think the theory would be the same in C#


Private propertylist() As String = New String() {"myname", "mytext"}

Private Sub Setme(ByVal params() As String)
For i As Integer = 0 To UBound(params)
myObject.GetType().GetProperty(propertylist(i)).SetValue(myObject, StripQuotes(params(i)), Nothing)
Next
End Sub

]]>