简体   繁体   中英

Convert C# to VB.NET: Name of field or property being initialized in an object initializer must start with '.'

i found a solution to my problem in github here
https://github.com/brandon-barker/PushBots.NET
but in C#, I tried to convert it in many site but they all throw error here's the original C# code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using PushBots.NET;
using PushBots.NET.Models;
using PushBots.NET.Enums;
using System.Threading.Tasks;

public partial class Default3 : System.Web.UI.Page
{
        protected void Button1_Click(object sender, EventArgs e)
        {
            Run().Wait();
        }
        static async Task Run()
        {
            var client = new PushBotsClient("55165801f85f8b457d", "b03052506824b4f3165ecc0");
            var pushMessage = new BatchPush()
            {
                Message = "new Test from API",
                Badge = "+1",
                Platforms = new[] { Platform.Android, Platform.iOS }
            };
            var result = await client.Push(pushMessage);
        }
}

it work fine but when i convert the static async Task Run() to VB i get alot of error here's the code in VB

Private Shared Function Run() As Task
    Dim client = New PushBotsClient("55165801f85f8b457d", "b03052506824b4f3165ecc0")
Dim pushMessage = New BatchPush() With { _
    Key .Message = "new Test from API", _
    Key .Badge = "+1", _
    Key .Platforms = New () {Platform.Android, Platform.iOS} _
}
    Dim result = Await client.Push(pushMessage)
End Function

the error i'm getting is on Key (Name of field or property being initialized in an object initializer must start with '.'.)

2nd error is with Await (Await can only be used within an Async method)

There are a few things to fix:

  • Add Async in the method declaration:

     Private Shared Async Function Run() As Task 
  • Remove Key from the With block. Adding Key during the C#->VB conversion is correct for anonymous classes , but not for non-anonymous class property initializations.

    In other words,
    C# new { ID = 3 } corresponds to VB New With { Key .ID = 3 } , but
    C# new MyClass { ID = 3 } should become New MyClass With { .ID = 3 } .

  • New() is not required for implicitly typed arrays, ie .Platforms = {Platform.Android, Platform.iOS} suffices.

You need to add Async to your function signature.

Private Shared Async Function Run() As Task

https://msdn.microsoft.com/en-us/library/hh191564.aspx

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM