Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

If you try to compile the query below in Visual Basic .NET, it fails.

From x In {1, 2} Select x.ToString()

The error given by the compiler is:

Range variable name cannot match the name of a member of the 'Object' class.

There is nothing wrong with the equivalent C# query, though:

from x in new[]{1, 2} select x.ToString()

This does not happen with the ToString overload that takes a format (it is a member of Int32, not Object). It does happen with other members of Object, as long as they don't take an argument: with GetType and GetHashCode it fails; with Equals(object) it compiles.

Why is this restriction in place, and what alternatives can I use?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
805 views
Welcome To Ask or Share your Answers For Others

1 Answer

I couldn't tell you exacly why VB has this ridiculousness, but the simple workaround is to put the method call in parens.

Dim q = From x In {1, 2} Select (x.ToString())

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...