Gordon Dugan
Gordon Dugan

Reputation: 129

using key for Object Initializers in VB.net

I am using some code from the PayPal Net SDK (available on nuget). I lifted it from the samples, which are written in C#. Then I used an automatic translator (from Telerik) to convert it to VB.net. The translator is not perfect, and here is some code it cannot handle:

 items = new List<PayoutItem>
               {
                     new PayoutItem
                     {
                         recipient_type = PayoutRecipientType.EMAIL,
         Amount = New Currency
                         {
                             value = "0.99",
         Currency = "USD"
                         },
                         receiver = "[email protected]",
                         note = "Thank you.",
         sender_item_id = "item_1"
                     },

                 }
             };

On looking at this code fragment, I thought it was an object initializer for a named class, but when I do a search for the class named 'payoutitem', I don't find it, and when I right click and go to the definition, It tells me it is reconstructing it out of "metadata" and gives me code that starts with:

using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using System;

namespace PayPal.Api
{
    public class PayoutItem : PayPalSerializableObject
    {
        public PayoutItem();

So I go ahead and use the translator to VB, and there too, I am told that the 'payoutItem' only exists as metadata. In addition, it gives me an error when I try to compile the result, which is:

Dim payout = New Payout() With { _
                key .sender_batch_header = New PayoutSenderBatchHeader() With { _
                    Key .sender_batch_id = "batch_" + System.Guid.NewGuid().ToString().Substring(0, 8), _
                    Key .email_subject = "You have a payment" _
                }, _
                Key .items = New List(Of PayoutItem)() From { _
                    New PayoutItem() With { _
                        Key .recipient_type = PayoutRecipientType.EMAIL, _
                        Key .amount = New Currency() With { _
                            Key .value = "0.99", _
                            Key .currency = "USD" _
                        }, _
                        Key .receiver = "[email protected]", _
                        Key .note = "Thank you.", _
                        Key .sender_item_id = "item_1" _
                    }, _
                    New PayoutItem() With { _
                        Key .recipient_type = PayoutRecipientType.EMAIL, _
                        Key .amount = New Currency() With { _
                            Key .value = "0.90", _
                            Key .currency = "USD" _
                        }, _
                        Key .receiver = "[email protected]", _
                        Key .note = "Thank you.", _
                        Key .sender_item_id = "item_2" _
                    }, _
                    New PayoutItem() With { _
                        Key .recipient_type = PayoutRecipientType.EMAIL, _
                        Key .amount = New Currency() With { _
                            Key .value = "2.00", _
                            Key .currency = "USD" _
                        }, _
                        Key .receiver = "[email protected]", _
                        Key .note = "Thank you.", _
                        Key .sender_item_id = "item_3" _
                    } _
                } _
            }

Apparently having the first 'Key' here causes problems, and when I remove it, I get other compile problems.

Can anyone explain to me why a class exists only as MetaData, even though it has a name (PayoutItem), and secondly, why the VB version does not compile? Thanks. Gordon

Upvotes: 2

Views: 850

Answers (2)

JoshYates1980
JoshYates1980

Reputation: 3626

Remove the Key attribute. Example of object setting the string attributes:

Dim lookup = New Lookup() With {.Street = "7 Clayton Street",
                                .City = "Montgomery",
                                .State = "AL"}

Upvotes: 1

mclark1129
mclark1129

Reputation: 7592

The reason you are seeing the metadata for the PayoutItem is because it is a C# library that you are referencing from VB.NET. You do not need to try and translate that code for any reason. I don't believe it's used at all for compilation, and I expect is mainly for use with Intellisense.

PayoutItem is indeed a real .NET class that exists in the PayPal SDK assembly. If it wasn't, then List<PayoutItem> wouldn't even be valid syntax.

The appropriate way to convert your first code block to VB.NET is

Dim items = New List(Of PayoutItem)({
    New PayoutItem() With { 
        .recipient_type = PayoutRecipientType.EMAIL,
        .Amount = New Currency() With {
            .value = "0.99",
            .Currency = "USD"
        },
        .receiver = "[email protected]",
        .note = "Thank you.",
        .sender_item_id = "item_1"
   }
})

Important things to take away here is that VB.NET uses the With keyword with its inline object initializers and also a preceding period (.) before the property names, where C# does not.

Also, please allow me a few moments of soapboxery while I say that the problem you are having is exactly why blindly using C# to VB.NET converters is a terrible idea. It is important that you have a strong understanding of the syntactical differences between the two languages, and also what exactly is happening in the code you are trying to convert. Not having one or both of those things leads to poorly written code or buggy code that you will have a much more difficult time debugging.

Upvotes: 2

Related Questions