Please explain the C # syntax on vb-er

I have the following piece of code:

        // Notify the source (the other control).
        if (operation != DropOperation.Reorder) {
            e = new DroppedEventArgs()
            {
                Operation = operation == DropOperation.MoveToHere ? DropOperation.MoveFromHere : DropOperation.CopyFromHere,
                Source = src,
                Target = this,
                DroppedItems = srcItems
            };
            src.OnDropped(e);
        }

      

I do not understand Operation = operation == DropOperation.MoveToHere ? DropOperation.MoveFromHere : DropOperation.CopyFromHere

.

Can someone explain this? For the record ... dropOperation is an enumeration. Can you give the syntactic equivalent of vb everything I need.

Set

+2


source to share


10 replies


The reason it is difficult to understand is because you are not familiar with the ternary operator?:. Basically what it does is evaluate the expression and return one of two values ​​depending on whether the evaluation returned true or false.

For example, the following expression will return "true" if the boolean value is true and "false" is:

bool test = false;
string testString = test ? "true" : "false";

      

It actually exists in VB.NET as well, although it is expressed in a slightly different way. These two statements respectively C # and VB.NET are actually the same



Dim s As String = If(True, "kek", "lol")
string s = true ? "kek" : "lol";

      

The difference between IIf and the tenary operator is that IIf will always evaluate both the second and third parameter, because IIf is a function instead of an operator. For this reason, the promising operator prefers.

Note. The tenary operator was added in VB 9, so if you are using previous versions you will have to rely on the IIF function for this function.

+15


source


If (operation = DropOperation.MoveToHere) Then
    Operation = DropOperation.MoveFromHere
Else
    Operation = DropOperation.CopyFromHere
End If

      



+7


source


Required link for wikipedia . I refused to mention this link in the comment, so here it is in the answer. Can you replace the usage? operator with IIF function calls:

Operation = IIF(operation = DropOperation.MoveToHere, DropOperation.MoveFromHere, DropOperation.CopyFromHere)

      

Note that they are not strictly equivalent as the IIF evaluates both true and false case, then how? Operator only evaluates the case it returns.

+3


source


This is sort of the equivalent IIf

in VB.NET (see Brian's comment):

Operation = IIf(operation = DropOperation.MoveToHere, _
                DropOperation.MoveFromHere, _
                DropOperation.CopyFromHere)

      

In C #, this is called a conditional statement and is sort of a shortcut to a simple if / else statement.

+3


source


The ?

operator is
used for conditional assignment . This line is basically syntactic sugar for:

// C# expanded example
if (operation == DropOperation.MoveToHere)
{
   Operation = DropOperation.MoveFromHere;
}
else
{
   Operation = DropOperation.CopyFromHere;
}

      

Which in VB would be equivalent to:

If operation = DropOperation.MoveToHere Then
   Operation = DropOperation.MoveFromHere
Else
   Operation = DropOperation.CopyFromHere
End If

      

+2


source


This is a conditional operator, it is very similar to the VB function IIf

:

Returns one of two objects, depending on the evaluation of the expression.

Public Function IIf( _
   ByVal Expression As Boolean, _ 
   ByVal TruePart As Object, _ 
   ByVal FalsePart As Object _ 
) As Object

      

In this particular example, the function IIf

would be written like this:

Operation = IIF((operation = DropOperation.MoveToHere), _
    DropOperation.MoveFromHere, _
    DropOperation.CopyFromHere)

      

+2


source


operation == DropOperation.MoveToHere ? DropOperation.MoveFromHere : DropOperation.CopyFromHere

      

This is called the ternary operator. This is basically a short way of writing:

if (operation == DropOperation.MoveToHere)
  return DropOperation.MoveToHere;
else
  return DropOperation.CopyFromHere;

      

+1


source


The construct ?:

is a ternary operator, mostly inline if (x) y else x

. The advantage of the inline interface is visible here, as it is immediately assigned to a variable. You cannot do this with an if statement.

+1


source


C # Bloggers use "?" lot. Check out this code:

int Foo(int x, int y){
return x==y? 10: 11;
}

      

Is equal to:

int Foo(int x, int y){
if (x==y)
return 10; 
else
return 11;
}

      

Just read Donut's well explained answer!

("VB-er" I like the term)

+1


source


It's called the ternary operator. I don't think it exists in VB, but it's basically a shorthand for if / else.

0


source







All Articles