C# to VB conversion issues

2.9k views Asked by At

Hi I am trying to convert some code from C# to VB but I have a couple of problems because of my lack of knowledge of C# (or perhaps VB). I am hoping somebody with more skill can put me right.

I am using the conversion tool here: http://www.developerfusion.com/tools/convert/csharp-to-vb/ to convert some code found here: http://www.urmanet.ch/?p=11

The code is designed to make a deep copy of EF entities.

After correcting the && on line 87, the conversion completes OK. A few imports later and some simple errors fixed, the remaining errors I could not solve are as follows:

1: The line

<System.Runtime.CompilerServices.Extension()> _
    Public Shared Function Clone(entityObject As EntityObject) As EntityObject

is flagged saying that extension methods can only be defined in modules. It is defined within a class but I presume this worked in C# so why would I get this error in VB?

2: There are quite a number of lines like this:

For Each [property] As var In properties

where the 'as var' section is flagged as being not defined; I'm not entirely sure what 'as var' means, can I just remove this and allow the compiler to infer the type?

3: There is an error in this line of code:

Dim t = GetType(EntityCollection(Of )).MakeGenericType(New () {[property].PropertyType.GetGenericArguments()(0)})

there is a squiggle under the '(' after 'New' which says Type Expected, I have no idea what to make of this line, and hence how to fix it, is somebody able to clarify it a bit?

4: The line

Private Shared ReadOnly _tracking As New List()

informs me that there are too few type arguments to System.Collections.Generic.List(Of T). Is this because of some difference between C# and VB?

Thanks very much for any advice.

3

There are 3 answers

2
Cody Gray - on strike On BEST ANSWER

Questions like this are far easier to answer if you post the original C# code, rather than just your failed attempts at conversion. As it is now, we can only guess at what the code is supposed to do, rather than doing the conversion ourselves.

But I'll take a stab at guessing anyway...

  1. Presumably, the code in question was actually declared in a static class in C#. Those are called "modules" in VB.NET for compatibility reasons. They work exactly the same way; trust the compiler's suggestion and move your extensions methods into modules.

  2. Declaring a variable as var is how you do anonymous typing in C#. The equivalent in VB.NET is turning on Option Infer in your project's Properties. You will need to remove the as var declaration, and then the compiler will automatically infer the variable's type, just as in C#.

  3. I have no idea what this line of code is intended to do, as you haven't shown the original working code in C#, but you need to remove New and change the way that you declare the array:

    Dim t = GetType(EntityCollection(Of )).MakeGenericType([property].PropertyType.GetGenericArguments()(0))
    
  4. No, this is not caused from a difference between VB.NET and C#. It's because you've used the generic List(Of T) class, but failed to specify the type you want to use. For example, your code should look like the following:

    Private Shared ReadOnly _tracking As New List(Of Integer)
    
2
Scott M. On
  1. go here to see how extension methods work in VB. It may be tedious to manually convert these if there are a lot of them. maybe you should consider leaving them in C#, as the code compiles to the same intermediate language.

  2. var means the compiler infers the type.

  3. in C# you can say `new[] { 1, 2, 3 } and the compiler will infer the type of the array for you based on the elements, but I don't know if this works in VB.

  4. you must put the type in the parentheses to create the correct generic type. Something like New List(string)

2
Gabe On

For #3, the MakeGenericType method expects a variadic array of Type. You can accomplish this with Dim t = GetType(EntityCollection(Of )).MakeGenericType([property].PropertyType.GetGenericArguments()(0))

For #4, I can't imagine how that could compile in C# either. However, looking at the full source it appears as though Private Shared ReadOnly _tracking As New List(Of SelfReferencesTracking) is the correct thing.