I have converted some C# code to VB.net
C# code:
private static List<Hotels> LoadData()
{
List<Hotels> lst = new List<Hotels>();
DataTable dt = new DataTable();
var bl = new BAL();
dt = bl.GetDataTable("tbl_Hotel");
foreach (DataRow dr in dt.Rows)
{
lst.Add(new Hotels { Id = Convert.ToInt32(dr["PId"]), HotelName = Convert.ToString(dr["HotelName"]) });
}
return lst;
}
Code converted to VB:
Private Shared Function LoadData() As List(Of Makes)
Dim bl As New BAL
Dim lst As New List(Of Makes)()
Dim dt As New DataTable()
dt = bl.GetDataTable2()
For Each dr As DataRow In dt.Rows
lst.Add(New Makes() With { _
'Here an error occur on "Key" Word
Key .Id = Convert.ToInt32(dr("ID")), _
Key .MakeName = Convert.ToString(dr("CHASSIS_NO")), _
Key .Model = Convert.ToString(dr("MODEL")), _
Key .Color = Convert.ToString(dr("color")), _
})
Next
Return lst
End Function
Error which I am facing is
Name of field or property being initialized in an object initializer must start with '.'.
I mostly use Asp.net C# and almost vb and C# can be understood but some point like this I am not getting the point why I am getting this error plz help as I am a newbie in vb.net
You only use Key
with anonymous types - not object initializers. So your VB code should be:
lst.Add(New Makes() With { _
.Id = Convert.ToInt32(dr("ID")), _
.MakeName = Convert.ToString(dr("CHASSIS_NO")), _
.Model = Convert.ToString(dr("MODEL")), _
.Color = Convert.ToString(dr("color")), _
})
On an anonymous type, Key
makes the property read-only and means it's part of the Equals
/ GetHashCode
implementations. Neither of those differences is relevant in a named type.
See more on this question at Stackoverflow