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.
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...
Presumably, the code in question was actually declared in a
staticclass 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.Declaring a variable
as varis how you do anonymous typing in C#. The equivalent in VB.NET is turning onOption Inferin your project's Properties. You will need to remove theas vardeclaration, and then the compiler will automatically infer the variable's type, just as in C#.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
Newand change the way that you declare the array: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: