【问题标题】:Visual Basic XML Literals Embedded Expressions - Can I do simple math?Visual Basic XML Literals Embedded Expressions - 我可以做简单的数学吗?
【发布时间】:2015-02-10 01:29:23
【问题描述】:

我被我希望是一个简单的问题难住了。我正在尝试嵌入一个简单的公式,增加一个变量以充当 XML 文档的“行号”。在 Visual Basic 中编写文字 XML。代码如下所示:

<%= 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>
                                       %>

我希望 OrderData 中的 d 的每次迭代都勾选 i + 1,但是,它只是返回“false”。在此处查看输出 XML:

<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>

有可能做这种事情吗?我什至尝试调用一个函数:

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

但这也会导致输出为“false”。我在这里想念什么? 提前感谢您的帮助!

Visual Studio 2013

编辑—— 这是我所指的功能:

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

【问题讨论】:

  • 这就是我对内联版本的期望;请记住,VB 对 assignmentcomparison 都使用=,并且使用哪个取决于它是 statement 还是 的上下文i>expression - 因为它试图形成一个表达式,所以它比较 ii+1 并得出结论它们不相等。没有看到你是如何定义函数的,就不能说函数一。
  • 私有函数 incrementI(i As Integer) Return i = i + 1 End Function
  • 是的,所以您再次在需要 表达式 的上下文中使用它,因此它将执行比较。如果您定义了函数的返回类型,编译器会帮助解决这个问题。而且,因为参数不是通过引用传递的,所以它也不会产生任何副作用(所以外部代码中的 i 保留了它在调用函数之前的任何值)。

标签: xml vb.net visual-studio linq-to-xml xml-literals


【解决方案1】:

如果您定义IncrementI 以通过引用而不是通过值接受其参数,您可以这样写:

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

生成此 XML:

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

正如 cmets 中所指出的,Visual Basic 使用 = 来进行赋值和相等。如果您在需要 表达式 的上下文中使用它,您将获得返回 TrueFalse 的相等比较。

如果您打开 Option Strict 并定义函数返回类型,那么编译器会帮助您看到您的函数没有修复问题:

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

当 Option Strict 开启时,上面会产生错误

Option Strict On 禁止从“布尔”到“整数”的隐式转换。

【讨论】:

  • 谢谢。作为一个业余程序员,有一些像“ByRef”这样的东西我认为是理所当然的,不知道目的是什么。我现在知道了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-11-14
  • 1970-01-01
  • 1970-01-01
  • 2017-01-08
  • 2014-04-23
  • 2022-12-10
  • 1970-01-01
相关资源
最近更新 更多