1

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 I'm using Net4: http://pastebin.com/TYgS3Ys3

4
  • As is, the code doesn't make much sense. String.Join("", "") seems broken -- there's nothing to join, and nothing to join it with. Why doesn't that return the same thing as "" in C#? Commented Aug 18, 2010 at 9:30
  • Empty string is just an example. Problem is with calling static methods with generic from String class in VB.Net. In C# it works. Commented Aug 18, 2010 at 9:38
  • In VB it works too -- you just don't need to tell it to. Commented Aug 18, 2010 at 9:45
  • It works even with specified T, but only with my custom class. The question is why I can't do it with String? Commented Aug 18, 2010 at 10:05

5 Answers 5

2

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.)

Sign up to request clarification or add additional context in comments.

11 Comments

@fessguid: It sounds dumb, but did you try restarting Visual Studio? The VB compiler is not perfect; I have encountered bugs before.
@fessguid: Also, are you sure you have your VB.NET project set to build for .NET 4.0?
@fessguid: Runtime version is not necessarily framework version -- and need not correspond to the references in your project. You can target runtime v4.0 and still reference framework v2.0, for example. Check your references.
Allright here my settings: lh3.ggpht.com/_yqm2MItZlHw/TGvClurdR-I/AAAAAAAAALk/jdc2e1h8J5M/… , I checked it on two machines. It's default ConsoleProject from VS2010. Have you tried to compile it?
Also this screenshot is useless because String in mscorlib library :)
|
0

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

1 Comment

Yeah, this is almost an answer to MY question
0

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() {""}))

5 Comments

Chr(0) would also return a nul char -- and the intent is clearer.
Have you try to compile it? Because I'm still have the same error
You're mistaken. He's using .NET 4.0, which does have an overload that supports the parameters he's passing: msdn.microsoft.com/en-us/library/dd992421.aspx.
app.config from project root: pastebin.com/GMyCaHg1
if you want a string to be treated as a char instead of a string you can do this: "x"c The c after the closing quote makes it work as a char.
0

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.

1 Comment

I've create an string2 class and copied Join declaration to it: Console.WriteLine(string2.Join(Of Char)("", "")) It compiled and works.
0

Here the solution:

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

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

3 Comments

Do you have a global variable defined in some module somewhere that's actually called String?
It's not because of it. No - I haven't.
Makes me wonder: if you try to compile a .net 4 app using .net 3.5's compiler, but targeting the .net 4 runtime and mscorlib, would it try to use 3.5's notion of String when it sees the keyword without brackets?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.