tags:

views:

90

answers:

1

I am using Linq to convert an array of any object to a CSV list:

String.Join(",", (From item In objectArray Select item.ToString()).ToArray())

This is giving me the strange error: "Range variable name cannot match the name of a member of the 'Object' class."

I can get round it by wrapping the string in a VB StrConv method, with a setting of "Nothing":

String.Join(",", (From item In oArray Select StrConv(item.ToString(), VbStrConv.None)).ToArray())

However, this seems like a bit of a hack and I would like to avoid it.

Does anyone have any ideas when this problems occurs, and any better ways to get round it?

+3  A: 

Modify your code to:

String.Join(",", (From item In objectArray Select stringVal = item.ToString()).ToArray())

The problem is VB gives a name to the variable returned by Select clause. Implicitly, it tries to give the name ToString to item.ToString() which will clash with ToString method. To prevent so, you should explicitly specify a name (stringVal in above line).

Mehrdad Afshari
Thanks, that is very helpful. Much appreciated