views:

345

answers:

10

Hello,

I have the following code snippet:

  // 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 the
Operation = operation == DropOperation.MoveToHere ? DropOperation.MoveFromHere : DropOperation.CopyFromHere line.

Can someone explain it? For the record...dropOperation is an enum. Can you give vb syntactical equivalent is all I need.

Seth

+2  A: 

This is using the ? operator 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
Donut
+6  A: 
If (operation = DropOperation.MoveToHere) Then
 Operation = DropOperation.MoveFromHere
Else
 Operation = DropOperation.CopyFromHere
End If
Cristian Ciupitu
Sorry for downvote, but Qua's answer should really score above this one.
Joel Coehoorn
@Joel Coehoorn: that's the strangest reason I've ever heard, but given the fact that I've lost only 2 points, I won't make a fuss about it :-)
Cristian Ciupitu
I always come back and check these. Do a no-op edit to your post and I can undo the downvote now.
Joel Coehoorn
@Joel Coehoorn: done.
Cristian Ciupitu
+1  A: 
operation == DropOperation.MoveToHere ? DropOperation.MoveFromHere : DropOperation.CopyFromHere

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

if (operation == DropOperation.MoveToHere)
  return DropOperation.MoveToHere;
else
  return DropOperation.CopyFromHere;
Aistina
There's no `==` in Basic.
Cristian Ciupitu
@Cristian: His response is C# code. It's fine.
Brian
A: 

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

BFree
+2  A: 

This is the conditional operator, it is very similar to VB's IIf function:

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

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

In this particular example the IIf function would be written like this:

Operation = IIF((operation = DropOperation.MoveToHere), _
    DropOperation.MoveFromHere, _
    DropOperation.CopyFromHere)
Andrew Hare
Shouldn't use the IIF function, as it evaluates both expressions. The tenary operator doesn't.
Qua
VB9 has a new ternary If operator that's a better match.
Joel Coehoorn
@Qua: It's fine in this case. The true and false part are just enums.
Brian
That's true, and without focusing too much on optimization it's worth mentioning that the tenary operator is a ... operator which means no function overhead as opposed to IFF
Qua
+1  A: 

The ?: construct is the ternary operator, basically an inline if (x) y else x. The benefit of the inline is seen here in that it is assigned immediately to a variable. You can't do that with an if statement.

pbh101
+3  A: 

Obligatory wikipedia link. I gave up on mentioning this link in a comment, so here it is in an answer. You can replace uses of the ? operator with calls to the IIF function:

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

Note that they are not strictly equivalent, since the IIF function evaluates both the true and the false case, whereas the ? operator only evaluates the case it returns.

Brian
If() is strictly equivalent though. There's no reason to use Iif unless you're stuck with an old version of VB. Iif is also not type safe.
Meta-Knight
+3  A: 

It is sort of equivalent of the IIf function in VB.NET (see Brian's comment):

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

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

Fredrik Mörk
Note that they are not strictly equivalent, since the IIF **function** evaluates both the true and the false case, whereas the ? **operator** only evaluates the case it returns.
Brian
@Brian: hence the text *sort of equivalent*, but still your comment adds good value.
Fredrik Mörk
If() is strictly equivalent though. There's no reason to use Iif unless you're stuck with an old version of VB. Iif is also not type safe.
Meta-Knight
+13  A: 

The reason it's hard to understand is due to the fact that you're unfamiliar with the ternary operator ?:. Basically what it does is evaluate an expression, and return one of two value depending on whether the evaluation returned true or false.

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

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

It does in fact exist in VB.NET as well - expressed a bit differently though. These two statements in respectively C# and VB.NET are in fact 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 tenary operator is much to prefer.

Note: The tenary operator was added in VB 9, so if you're using previous versions you'll have to rely on the IIF function for this functionality.

Qua
Interesting, didn't know that (I have not coded VB.NET in a couple of years). This one is definitely to prefer over the `IIf` function given that is uses short-circuit evaluation.
Fredrik Mörk
+1 for suggesting If instead of Iif!!!! Also, If has the advantage of being type-safe, you don't need to do casts.
Meta-Knight
I think that you have a typo in the code. I don't see an `Iif`, only an `If`.
Cristian Ciupitu
It's not a typo, If() is VB9's ternary operator.
Meta-Knight
Thanks Qua...that did it. And thanks for the lesson on ternary op for vb. I am very familiar with iif. Glad to know there is now a short-cicuiting version.Seth
Seth Spearman
The real typo is in the first sentence of the answer: "tenary". ;)
Shea Daniels
Loving the random WoW reference...
Fiona Holder
+1  A: 

C# Bloggers use the "?" a lot. Look 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 the well explained Donut's answer!!

("VB-er" I like the term)

yelinna