1

I'm new to Silverlight and used to code in VB.Net before. Now I'm trying to assign RepeatButtons' Interval values in a single statement but it sets zero.

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

This works fine in c# but not in vb.net.

3
  • possible duplicate of Multiple Variables Commented Nov 7, 2012 at 15:16
  • Is your variabel interval == 0? Commented Nov 7, 2012 at 15:19
  • No it is an integer and it works when the properties are assigned one by one. Commented Nov 7, 2012 at 15:20

3 Answers 3

5

You're confusing VB.Net with C#.

You can't do what you're trying to do in VB.Net. You need to write multiple statements:

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

What happens in your case is that only the first equals sign is the assignment operator, the subsequent ones is the comparison operator. In equivalent C# it would be like this:

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

Which is clearly not what you wanted to do.

It also looks like you don't have Option Strict turned on (since the comparison operator returns a Boolean and Interval is likely an Integer, your code should show a compiler error with Option Strict On when assigning a Boolean to an Integer).

Sign up to request clarification or add additional context in comments.

1 Comment

+1 Especially for Option Strict. As a general rule Option Strict should be On. Can I also add some links to the relevant parts of the manual and the language spec.
3

I thought this feature was so cool in C# that I wrote a VB extension method (Assign) to do the same thing. The semantics are pretty easy to follow; you just call Assign on any value to assign it to multiple other variables i.e.

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

etc...

Here's the code:

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

Comments

2

Try this

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

The result is

0,0,0,0,-1

So it doens't work in VB.NET like in C#

I was curious to find the differences in the IL code from VB.NET and C#.
Looking at the IL code it is obvious the reason for the lack of support in VB.NET

VB.NET IL code

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 Code for an equivalent example

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     

The VB version goes on comparing the values, thus the problems is the = operator that in VB.NET has a double meaning. In this case is used to compare instead of to assign.

1 Comment

Correct answer but... wouldn't it be quicker to look in the manual or the language spec :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.