开发者

Write String.Join(Of T) in VB.Net

开发者 https://www.devze.com 2023-01-11 21:01 出处:网络
I have a simple code in C#: Console.WriteLine(string.Join<char>(\"\", \"\")); And I can\'t convert it to VB.Net. Even reflector show me code in VB like:

I have a simple code in C#:

 Console.WriteLine(string.Join<char>("", ""));

And I can't convert it to VB.Net. Even reflector show me code in VB like:

Console.WriteLine(String.Join(Of Char)("", ""))

But it can't be compiled becouse I have an starge error: Error 1 Expression expected. It looks like VB.Net don't have this generic method at all. Both project use Net Framework 4. Why this error happened?

UPD:

I've create a custom class and copy Join(Of T) declaration to it:

Class string2
Public Shared Function Join(Of T)(ByVal separator As String, ByVal values As System.Collections.Generic.IEnumerable(Of T)) As String
    Return "1"
End Function
End Class

Console.WriteLine(string2.Join(Of Char)("", ""))

It works

UPD2:

My compilation string, where you can see that 开发者_StackOverflowI'm using Net4: http://pastebin.com/TYgS3Ys3


Do you have a code element named String somewhere in your project?

Based on the answer you have added to this question (where you indicate that changing String to [String] appears to have solved the problem), I guessed that this may be the result of a naming collision.

I was able to duplicate the error you are seeing -- "Expression expected" -- by adding a module to my project called String and defining a (non-generic) Join method from within that module.

This may not be the specific scenario you find yourself in. But the fact that the code works for you with [String] is, to me, very compelling evidence of a simple namespace collision.


Based on the documentation for the "Expression expected" error, I'm guessing you haven't included the entire section of code where this error is appearing for you.

Do you have a lingering operator such as + or = somewhere?

(The VB.NET code you posted is indeed equivalent to the C# code above it and should compile no problem. This is why I suspect the real issue lies elsewhere.)


String.Join<T>(string, IEnumerable<T>) is useful with LINQ, for standard joins is better to use the String.Join(string, string()) overload.


In C#, "" as Char produces an empty Char (\0). Writing the same thing ("") in VB produces an empty string which is not the same as an empty char. In order to produce an empty character, you'll have to write New Char().

Your VB code therefore becomes:

Console.WriteLine(String.Join(Of Char)(New Char(), New Char()))

Edit

I just checked and it appears String.Join does not support the format you're specifying.
Instead, it goes as follows:

Join(separator As String, value As String()) As String

Your code should be as follows:

Console.WriteLine(String.Join("", New String() {""}))


String.Join(Of Char)(str1, str2) wasn't added til .net 4, it seems. That's why your custom class worked -- it had the declaration, but the String class in the framework you're actually using doesn't.

Check your settings and references to make sure you're targeting .net 4 all around -- cause that's the only thing that seems able at this point to stop the call from working.


Here the solution:

Console.WriteLine([String].Join(Of Char)("", ""))

Why this problem occurs only with generic method? I wish I know...

0

精彩评论

暂无评论...
验证码 换一张
取 消