tags:

views:

79

answers:

5

Hello!

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

A: 

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

munissor
Yeah, this is almost an answer to MY question
fessguid
A: 

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() {""}))
Alex Essilfie
`Chr(0)` would also return a nul char -- and the intent is clearer.
cHao
Have you try to compile it? Because I'm still have the same error
fessguid
@Alex: You're mistaken. He's using .NET 4.0, which does have an overload that supports the parameters he's passing: http://msdn.microsoft.com/en-us/library/dd992421.aspx.
Dan Tao
app.config from project root: pastebin.com/GMyCaHg1
fessguid
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.
Pondidum
A: 

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.

cHao
I've create an string2 class and copied Join declaration to it:Console.WriteLine(string2.Join(Of Char)("", ""))It compiled and works.
fessguid
@fessguid: updated.
cHao
+2  A: 

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

Dan Tao
Nope. Here the whole code: http://pastebin.com/WsJb06eQ
fessguid
@fessguid: It sounds dumb, but did you try restarting Visual Studio? The VB compiler is not perfect; I have encountered bugs before.
Dan Tao
@fessguid: Also, are you *sure* you have your VB.NET project set to build for .NET 4.0?
Dan Tao
app.config from project root:http://pastebin.com/GMyCaHg1
fessguid
@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.
cHao
Allright here my settings: http://lh3.ggpht.com/_yqm2MItZlHw/TGvClurdR-I/AAAAAAAAALk/jdc2e1h8J5M/s800/code.JPG , I checked it on two machines. It's default ConsoleProject from VS2010. Have you tried to compile it?
fessguid
Also this screenshot is useless because String in mscorlib library :)
fessguid
You can found a link on this page with WHOLE code, I have only one module.So, you say that in your project you don't have this problem and it's compiled fine, yeah?Have you tried to use Enum.TryParse method? Try it in VS2010 using VB.Net and you'll see that IDE automatically add [] to Enum. Without them you can't do it. With String the same thing
fessguid
@fessguid: I had a single VB file with one class, no explicit references. Compiling it with .net 3.5's vbc.exe gave me the exact same error you're talking about, but compiling with .net 4's worked fine. (I didn't declare references, cause, well, it was a single VB file -- not a VS project.) So it seems the problem is that somewhere, somehow, the compiler is looking at the wrong String class.
cHao
@fessguid: The IDE most likely adds brackets because "Enum" and "String" are keywords in VB (even if they correspond to classes with the same names). But then again, i didn't have any problems with "String" with no brackets.
cHao
@cHao: I wonder if it's somehow possible he's got a reference to both mscorlib.dll files (3.5 and 4.0)?
Dan Tao
@Dan Tao: Seems unlikely -- i'd think the compiler would freak about two nearly identical namespaces, esp if they're being used by the code -- but there's very few plausible scenarios as of yet.
cHao
@cHao: Agreed -- quite bizarre.
Dan Tao
A: 

Here the solution:

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

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

fessguid
Do you have a global variable defined in some module somewhere that's actually *called* `String`?
Dan Tao
It's not because of it. No - I haven't.
fessguid
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?
cHao