I am trying to convert a block of c# to vb.
I used the service at developerfusion.com to do the conversion, but when I paste it into Visual Studio, it is complaing about the "Key" statements("Name of field or property being initialized in an object initializer must start with '.' ").
I played around with the code for a few hours trying to get around that, but everything I did only led to more errors.
So I started to wonder if the conversion at developerfusion was ever correct.
Here is the c# to vb.net.
I am not sure where "Key" is coming from and was wondering if someone could enlighten me.
Thanks!
From
var combinedResults =
cars.Select(c=>new carTruckCombo{ID=c.ID,make=c.make,model=c.model})
.Union(tracks.Select(t=>new carTruckCombo{ID=t.ID,make=t.make,model=t.model}));
To
Dim combinedResults = cars.[Select](Function(c) New carTruckCombo() With { _
Key .ID = c.ID, _
Key .make = c.make, _
Key .model = c.model _
}).Union(tracks.[Select](Function(t) New carTruckCombo() With { _
Key .ID = t.ID, _
Key .make = t.make, _
Key .model = t.model _
}))
Remove the Key
do this instead:
Dim combinedResults = cars.Select(Function(c) New carTruckCombo() With { _
.ID = c.ID, _
.make = c.make, _
.model = c.model _
}).Union(tracks.Select(Function(t) New carTruckCombo() With { _
.ID = t.ID, _
.make = t.make, _
.model = t.model _
}))
As a side-note, this converter always worked better for me whenever i needed it:
http://converter.telerik.com/
In C#, when creating an anonymous type, it generates an Equals
and GetHashCode
implementation for you using all the properties of your anonymous type.
VB.NET does something similar, but it requires you to put the Key
modifier on the properties of your anonymous type.
C# "just does it" where VB.NET gives you the flexibility to define which properties are used in equality. Since C# uses all of the properties, the converter is giving you Key
on everything so equality works the same.
OK, so that's the backstory of the Key
modifier, so what's wrong with your conversion?
The converter seems to incorrectly assume you are using an anonymous type, but you're not. Your type is carTruckCombo
so they Key
modifiers don't work. Removing the Key
modifier would fix the problem since you have a well defined class where you can implement your equality there.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With