0

我是 Silverlight 的新手,以前在 VB.Net 中编写过代码。现在我试图在单个语句中分配 RepeatButtons 的 Interval 值,但它设置为零。

rbtUp.Interval = rbtLeft.Interval = rbtCenter.Interval = rbtRight.Interval
= rbtDown.Interval = interval

这在 c# 中可以正常工作,但在 vb.net 中不行。

4

3 回答 3

5

您将 VB.Net 与 C# 混淆了。

你不能在 VB.Net 中做你想做的事情。您需要编写多个语句:

rbtUp.Interval = interval
rbtLeft.Interval = interval
rbtCenter.Interval = interval
rbtRight.Interval = interval
rbtDown.Interval = interval

在您的情况下发生的情况是,只有第一个等号是赋值运算符,后面的等号是比较运算符。在等效的 C# 中,它会是这样的:

rbtUp.Interval = rbtLeft.Interval == rbtCenter.Interval == rbtRight.Interval == rbtDown.Interval == interval;

这显然不是你想做的。

看起来您还没有打开 Option Strict(因为比较运算符返回一个布尔值并且 Interval 可能是一个整数,所以在将布尔值分配给整数时,您的代码应该显示带有 Option Strict On 的编译器错误)。

于 2012-11-07T15:31:55.030 回答
2

尝试这个

Sub Main
    Dim outer As Integer = -1
    Dim inner1 As Integer
    Dim inner2 As Integer
    Dim inner3 As Integer
    Dim inner4 As Integer

    inner1 = inner2 = inner3 = inner4 = outer
    Console.WriteLine("{0},{1},{2},{3},{4}", inner1, inner2, inner3, inner4, outer)

End Sub

结果是

0,0,0,0,-1

所以它在 VB.NET 中不像在 C# 中那样工作

我很想知道 VB.NET 和 C# 中 IL 代码的不同之处。
查看 IL 代码很明显是 VB.NET 缺乏支持的原因

VB.NET IL 代码

IL_0001:  ldc.i4.m1   
IL_0002:  stloc.s     04 
IL_0004:  ldloc.1     
IL_0005:  ldloc.2     
IL_0006:  ceq         
IL_0008:  ldc.i4.0    
IL_0009:  cgt.un      
IL_000B:  neg         
IL_000C:  ldloc.3     
IL_000D:  ceq         
IL_000F:  ldc.i4.0    
IL_0010:  cgt.un      
IL_0012:  neg         
IL_0013:  ldloc.s     04 
IL_0015:  ceq         
IL_0017:  ldc.i4.0    
IL_0018:  cgt.un      
IL_001A:  neg         

等效示例的 C# IL 代码

IL_0001:  ldc.i4.m1   
IL_0002:  stloc.0     
IL_0003:  ldloc.0     
IL_0004:  dup         
IL_0005:  stloc.s     04 
IL_0007:  dup         
IL_0008:  stloc.3     
IL_0009:  dup         
IL_000A:  stloc.2     
IL_000B:  stloc.1     

VB 版本继续比较值,因此问题是 = 运算符在 VB.NET 中具有双重含义。在这种情况下,用于比较而不是分配。

于 2012-11-07T15:28:58.977 回答
2

我认为这个功能在C#中非常酷,所以我编写了一个VB扩展方法 ( ) 来做同样的事情。语义很容易理解;您只需调用任何值即可将其分配给多个其他变量,即AssignAssign

Call True.Assign(b1, b2, b3)
Call 4.1.Assign(d1, d2, d3)

ETC...

这是代码:

Imports System.Runtime.CompilerServices


Namespace Utility
    Public Module MultiAssignExtensionMethod
        ' Multiply assign the same value to 1 (required) or more variables
        <Extension()> _
        Public Function Assign(Of T)(this As T, ByRef first As T, Optional ByRef second As T = Nothing, Optional ByRef third As T = Nothing,
                                    Optional ByRef forth As T = Nothing, Optional ByRef fifth As T = Nothing, Optional ByRef sixth As T = Nothing,
                                    Optional ByRef seventh As T = Nothing, Optional ByRef eighth As T = Nothing, Optional ByRef nineth As T = Nothing,
                                    Optional ByRef tenth As T = Nothing) As T
                            ' I would LIKE to do this as a ParamArray, but it doesn't allow references for basic types....grrr
            first = this
            second = this
            third = this
            forth = this
            fifth = this
            sixth = this
            seventh = this
            eighth = this
            nineth = this
            tenth = this

            Return this     ' For use as assignment and evaluation as Function parameter
        End Function
    End Module
End Namespace
于 2015-10-12T18:24:33.177 回答