-1

I have an existing VB. Net application I am trying to add functionality to. The example code is in C# and I am not sure how to convert this section. I have converted the rest of it but stuck on this....

        var parameters = new CreateSMSMessage();
        parameters.from = new MessageStoreCallerInfoRequest { phoneNumber = "19998887777"};
        parameters.to = new MessageStoreCallerInfoRequest[] 
            { 
            new MessageStoreCallerInfoRequest 
                {
                phoneNumber = "12223334444"
            } };
        parameters.text = "Hello World from C#";

        var resp = await restClient.Restapi().Account().Extension().Sms().Post(parameters);
  • It's the same thing, except `[]` becomes `()` and you don't need the semicolons. – Jimi Feb 08 '22 at 04:38
  • you can compile it then decompile to vb.net(maybe a bit difference but largely the same) – Lei Yang Feb 08 '22 at 04:39
  • 2
    Why not keep the C#, and compile it to its own assembly, and reference it from the VB.NET project? VB.NET is a dead-language-walking at this point, so I strongly caution anyone thinking of continuing to use VB.NET today, let alone _migrating_ C# to VB.NET. – Dai Feb 08 '22 at 04:40
  • 1
    I appreciate the feedback - I found that structure in C# is an object initializer - for VB format is.... parameters.from = New MessageStoreCallerInfoRequest() With {.phoneNumber = "12143462259"} – Brian Kleinfall Feb 08 '22 at 04:52
  • even if vb don't have initializer grammer, it is very easy to write, just seperate the construction and property value assigning to two statements. – Lei Yang Feb 08 '22 at 04:57
  • @LeiYang can you give me an example - especially for the array for the parameters.to? – Brian Kleinfall Feb 08 '22 at 05:15
  • [How to: Initialize an Array Variable in Visual Basic](https://learn.microsoft.com/en-us/dotnet/visual-basic/programming-guide/language-features/arrays/how-to-initialize-an-array-variable) – Lei Yang Feb 08 '22 at 05:33
  • this can be converted using an c# to vb code converter – Anu6is Feb 08 '22 at 11:41
  • @Dai Post mentions adding functionality to existing VB, so I don't think there is an assembly to be left in C#. Regardless of the merits of starting a new project in VB, I don't think it makes sense to rewrite working VB code unless there are serious maintenance issues. – Craig Feb 08 '22 at 14:37

1 Answers1

2

I assume the part you're having trouble with is this:

parameters.from = new MessageStoreCallerInfoRequest { phoneNumber = "19998887777"};
parameters.to = new MessageStoreCallerInfoRequest[] 
    {
        new MessageStoreCallerInfoRequest
        {
            phoneNumber = "12223334444"
        }
    };

VB has virtually identical array initializer syntax and slightly different member initializer syntax requiring the With keyword and prepending . on the member access.

The equivalent in VB is:

parameters.from = New MessageStoreCallerInfoRequest With {.phoneNumber = "19998887777"}
parameters.to = New MessageStoreCallerInfoRequest() 
    { 
        New MessageStoreCallerInfoRequest With {.phoneNumber = "12223334444"}
    }
Craig
  • 2,248
  • 1
  • 19
  • 23