Aspdotnet-Suresh

aspdotnet-suresh offers C#.net articles and tutorials,csharp dot net,asp.net articles and tutorials,VB.NET Articles,Gridview articles,code examples of asp.net 2.0 /3.5,AJAX,SQL Server Articles,examples of .net technologies

VB.NET - Name of field or property being initialized in an object initializer must start with '.'

Jun 8, 2014
Introduction:

Here I will explain how to solve the problem of “name of field or property being initialized in an object initializer must start with '.'” while converting C# code to vb.net.

Description:

In previous posts I explained many articles relating to solve errors in asp.net, SQL Server, IIS, etc. Now I will explain how to solve the problem of “name of field or property being initialized in an object initializer must start with '.'while converting C# code to vb.net.

I have one foreach loop code in C#  that is like as shown below

C# Code


foreach(DataRow dr in dt.Rows)
{
countrychart.PieChartValues.Add(new AjaxControlToolkit.PieChartValue
{
Category = dr["name"].ToString(),
Data = Convert.ToDecimal(dr["total"]),
});
}
I tried to convert C# to vb.net code by using some online tools once I converted code that will be like as shown below

VB.NET Code


For Each dr As DataRow In dt.Rows
countrychart.PieChartValues.Add(New AjaxControlToolkit.PieChartValue() With { _
Key.Category = dr("name").ToString(), _
Key.Data = Convert.ToDecimal(dr("total")) _
})
Next
Whenever I run above code I got error like “name of field or property being initialized in an object initializer must start with '.'

I finally found this error because I used Key with object initializers. To solve this problem I removed that Key name like as shown below then everything working fine


For Each dr As DataRow In dt.Rows
countrychart.PieChartValues.Add(New AjaxControlToolkit.PieChartValue() With { _
.Category = dr("name").ToString(), _
.Data = Convert.ToDecimal(dr("total")) _
})
Next

I hope it helps you to solve your problem. Happy Coding……

If you enjoyed this post, please support the blog below. It's FREE!

Get the latest Asp.net, C#.net, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email.

subscribe by rss Subscribe by RSS subscribe by email Subscribe by Email

0 comments :

Give your Valuable Comments

Note: Only a member of this blog may post a comment.

© 2015 Aspdotnet-Suresh.com. All Rights Reserved.
The content is copyrighted to Suresh Dasari and may not be reproduced on other websites without permission from the owner.