dcsimg
Results 1 to 6 of 6

Thread: Can someone help convert this code?

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Oct 2000
    Posts
    1,463

    Question Can someone help convert this code?

    Hello,

    I am using a lib for stripe.com and the example is only in C#. I converted some of it but can't get this line converted. Thanks!

    Dim myCustomer As var = New StripeCustomerCreateOptions

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    34,460

    Re: Can someone help convert this code?

    What did it look like when it was in C#, cause that isn't C#. The simple answer would be:

    Dim myCustomer As Object = New StripeCustomerCreateOptions

    but that's almost certainly not the best you can do. The problem is that "Dim VariableName As Type" is a VB construct. C# would use different syntax like "type VariableName". It's that 'type' part that would be really valuable.
    My usual boring signature: Nothing

  3. #3
    You don't want to know.
    Join Date
    Aug 2010
    Posts
    4,580

    Re: Can someone help convert this code?

    The only logical type 'var' could be is StripeCustomerCreateOptions.

    Code:
    Dim myCustomer As New StripeCustomerCreateOptions()
    But that one line by itself wasn't really in its C# form, so I can only speculate. And it's possible there's a reason its constructor needs special consideration in VB.
    This answer is wrong. You should be using TableAdapter and Dictionaries instead.

  4. #4

    Thread Starter
    Frenzied Member
    Join Date
    Oct 2000
    Posts
    1,463

    Re: Can someone help convert this code?

    Sorry, here is all of the code from the example that I need to convert. I tried it in the Telerik converter and failed to do all of it.

    Code:
    var myCustomer = new StripeCustomerCreateOptions();
    
    // set these properties if it makes you happy
    myCustomer.Email = "pork@email.com";
    myCustomer.Description = "Johnny Tenderloin (pork@email.com)";
    
    // setting up the card
    myCustomer.Source = new StripeSourceOptions()
    {
        // set this property if using a token
        TokenId = *tokenId*,
    
        // set these properties if passing full card details (do not
        // set these properties if you set TokenId)
        Object = "card",
        Number = "4242424242424242",
        ExpirationYear = "2022",
        ExpirationMonth = "10",
        AddressCountry = "US",                // optional
        AddressLine1 = "24 Beef Flank St",    // optional
        AddressLine2 = "Apt 24",              // optional
        AddressCity = "Biggie Smalls",        // optional
        AddressState = "NC",                  // optional
        AddressZip = "27617",                 // optional
        Name = "Joe Meatballs",               // optional
        Cvc = "1223"                          // optional
    };
    
    myCustomer.PlanId = *planId*;                          // only if you have a plan
    myCustomer.TaxPercent = 20;                            // only if you are passing a plan, this tax percent will be added to the price.
    myCustomer.Coupon = *couponId*;                        // only if you have a coupon
    myCustomer.TrialEnd = DateTime.UtcNow.AddMonths(1);    // when the customers trial ends (overrides the plan if applicable)
    myCustomer.Quantity = 1;                               // optional, defaults to 1
    
    var customerService = new StripeCustomerService();
    StripeCustomer stripeCustomer = customerService.Create(myCustomer);

  5. #5
    Fanatic Member
    Join Date
    Jan 2006
    Posts
    669

    Re: Can someone help convert this code?

    'var' in C# is for inferred typing.
    In VB, you place "Option Infer On" at the top of the file (or as a project-level setting), and then just leave off the type:

    Code:
    Option Infer On 'at the top of the file or in project settings
    
    Dim myCustomer = New StripeCustomerCreateOptions
    Note that the Option Infer On is very important - without it the type becomes 'Object'.
    David Anton
    Convert between VB, C#, C++, & Java
    www.tangiblesoftwaresolutions.com

  6. #6
    You don't want to know.
    Join Date
    Aug 2010
    Posts
    4,580

    Re: Can someone help convert this code?

    I guess you meant this line?
    Code:
    var customerService = new StripeCustomerService();
    var doesn't do quite the same thing as "As Object" in C#. Here's how it normally works.

    In VB, you declare a variable like this:
    Code:
    <Accessor|Dim> <name> As <TypeName>
    "Accessor" can be "Public", "Private", etc., and the rest is fairly obvious. In C#, variables are declared like this:
    Code:
    <Accessor> <TypeName|var> <name>
    It's mostly a reordering, what's different is C# has that possibility as "var" instead of the typename. What's that mean?

    It's not equivalent to "As Object", as SH's post might imply. It actually means "Please infer the type for me", and can't always be used. For example, in VB, these two lines are equivalent:
    Code:
    Dim name As String = "Bulbasaur"
    Dim otherName = "Bulbasaur"
    When the right hand side of an assignment expression has an unambiguous type, VB lets you drop the type declaration. That's a really language-oriented way of saying, "If the thing on the right can't be anything but String, you don't have to say 'As String'."

    var is C#'s way of saying the same thing. These two lines are equivalent:
    Code:
    string name = "Bulbasaur";
    var otherName = "Bulbasaur";
    Since the right-hand side is clearly a String, otherName is considered a String.

    So, the VB equivalent of that line could be any of these:
    Code:
    Dim customerService As StripeCustomerService = New StripeCustomerService()
    Dim customerService As New StripeCustomerService()
    Dim customerService = New StripeCustomerService()
    I think that most VB developers today write code like the middle line, and most C# developers pretending they write VB write it like the last line. There's no real good reason to write it like the first line.
    This answer is wrong. You should be using TableAdapter and Dictionaries instead.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width