Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/silverlight/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Silverlight VB.Net单行中的多个赋值_Vb.net_Silverlight_Variable Assignment - Fatal编程技术网

Silverlight VB.Net单行中的多个赋值

Silverlight VB.Net单行中的多个赋值,vb.net,silverlight,variable-assignment,Vb.net,Silverlight,Variable Assignment,我是Silverlight的新手,以前习惯用VB.Net编写代码。现在我试图在一条语句中分配RepeatButtons的间隔值,但它设置为零 rbtUp.Interval = rbtLeft.Interval = rbtCenter.Interval = rbtRight.Interval = rbtDown.Interval = interval 这在c#中很好,但在vb.net中不行。试试这个 Sub Main Dim outer As Integer = -1 Dim i

我是Silverlight的新手,以前习惯用VB.Net编写代码。现在我试图在一条语句中分配RepeatButtons的间隔值,但它设置为零

rbtUp.Interval = rbtLeft.Interval = rbtCenter.Interval = rbtRight.Interval
= rbtDown.Interval = interval
这在c#中很好,但在vb.net中不行。

试试这个

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         
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     
C#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         
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中的=运算符具有双重含义。在本例中,用于比较而不是赋值。

您将VB.Net与C#混淆了

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

rbtUp.Interval=Interval
rbtLeft.间隔=间隔
rbtCenter.Interval=Interval
rbtRight.Interval=间隔
rbtDown.Interval=Interval
在您的例子中,只有第一个等号是赋值运算符,后面的等号是比较运算符。在等效的C#中,它是这样的:

rbtUp.Interval=rbtLeft.Interval==rbtCenter.Interval==rbtRight.Interval==rbtDown.Interval==Interval;
这显然不是你想做的


看起来还没有启用Option Strict(由于比较运算符返回布尔值,并且间隔很可能是整数,因此在将布尔值赋给整数时,代码应显示Option Strict启用时的编译器错误).

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

等等

代码如下:

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
导入System.Runtime.CompilerServices
命名空间实用程序
公共模块多重分配扩展方法
'乘法将相同的值分配给1个(必需)或多个变量
_
(T的)公共函数赋值(此为T,ByRef第一个为T,可选ByRef第二个为T=Nothing,可选ByRef第三个为T=Nothing,
可选ByRef forth为T=Nothing,可选ByRef fifth为T=Nothing,可选ByRef fifth为T=Nothing,
可选ByRef第七个为T=Nothing,可选ByRef第八个为T=Nothing,可选ByRef第九个为T=Nothing,
可选ByRef(作为T=无)作为T
'我希望以ParamArray的形式执行此操作,但它不允许对基本类型进行引用。…grrr
第一个=这个
秒=这个
第三个=这个
forth=这个
第五个=这个
第六个=这个
第七个=这个
第八个=这个
九=这个
第十个=这个
将此“返回”用作赋值,并作为函数参数进行求值
端函数
端模块
结束命名空间

您的variabel interval==0是否可能重复?否,它是一个整数,在逐个分配属性时有效。回答正确,但。。。查看:)+1(尤其是
选项Strict
)会更快吗。一般来说,
选项Strict
应该是
On
。我也可以添加一些链接到和的相关部分。