Visual Basic XML Literals Embedded Expressions - Can I do simple math?

210 views Asked by At

I'm getting stumped by what I hope is a simple problem. I'm trying to embed a simple formula incrementing a variable to act as a "line number" for an XML document. Writing literal XML in visual basic. Here's what the code looks like:

<%= From d In orderData
                                       Select <ItemOut quantity=<%= d.OrderQuantity %> lineNumber=<%= i %>>
                                                  <ItemID>
                                                      <SupplierPartID><%= d.VendorPartNo %></SupplierPartID>
                                                  </ItemID>
                                                  <ItemDetail>
                                                      <UnitPrice>
                                                          <Money currency="USD"><%= d.PricePerPackage %></Money>
                                                      </UnitPrice>
                                                      <Description xml:lang="en"><%= d.Description %></Description>
                                                      <UnitOfMeasure><%= d.OrderUOM %></UnitOfMeasure>
                                                  </ItemDetail>
                                                  <%= i = i + 1 %>
                                              </ItemOut>
                                       %>

I was expecting each iteration of d in OrderData to tick i + 1, however, it is simply returning "false". See the output XML here:

<ItemOut quantity="1" lineNumber="1">
        <ItemID>
          <SupplierPartID>99999</SupplierPartID>
        </ItemID>
        <ItemDetail>
          <UnitPrice>
            <Money currency="USD">0.00</Money>
          </UnitPrice>
          <Description xml:lang="en">Tub and Tile Caulk Biscuit</Description>
          <UnitOfMeasure>cs</UnitOfMeasure>
        </ItemDetail>false</ItemOut>
      <ItemOut quantity="1" lineNumber="1">
        <ItemID>
          <SupplierPartID>999999</SupplierPartID>
        </ItemID>
        <ItemDetail>
          <UnitPrice>
            <Money currency="USD">0.00</Money>
          </UnitPrice>
          <Description xml:lang="en">Tub and Tile Caulk Almond</Description>
          <UnitOfMeasure>cs</UnitOfMeasure>
        </ItemDetail>false</ItemOut>

Is it possible to do this sort of thing? I even tried making a call to a function instead:

lineNumber=<%= incrementI(i) %>>

But that also results in "false" as the output. What am I missing here? Thank you for your help in advance!

Visual Studio 2013

Edit-- Here's the function I'm referring to:

Private Function incrementI(i As Integer)
    Return i = i + 1
End Function
1

There are 1 answers

1
Damien_The_Unbeliever On BEST ANSWER

If you define IncrementI to accept its parameter by reference rather than by value, you can write this:

Sub Main()
    Dim j As Integer = 0

    Dim x = <sample><thing><%= IncrementI(j) %></thing><thing><%= IncrementI(j) %></thing></sample>

    Console.WriteLine(x)
    Console.ReadLine()
End Sub

Function IncrementI(ByRef i As Integer) As Integer
    i = i + 1 'This is now a statement rather than an expression, so its assignment
    Return i
End Function

Which produces this XML:

<sample>
  <thing>1</thing>
  <thing>2</thing>
</sample>

As pointed out in the comments, Visual Basic uses = for both assignment and equality. If you use it in a context where an expression is required, you'll get an equality comparison that returns True or False.

If you turn on Option Strict and define the function return type, then the compiler would have helped you see that your function hadn't fixed matters:

'Broken code
Private Function incrementI(i As Integer) as Integer
    Return i = i + 1
End Function

When Option Strict is on, the above produces the error

Option Strict On disallows implicit conversions from 'Boolean' to 'Integer'.