0

I am converting a control (Code Project CheckBoxComboBox) from C# to VB. Mostly, I can figure out what the C# is doing and write the equivalent in VB. I have not been able to figure this piece out, though.

C# Code:

if ((Parent as Popup).ProcessResizing(ref m))

What is that code trying to do, exactly? I know it is trying to call the ProcessResizing function in the Popup class, but I am unsure about the Parent as Popup.

Dave Johnson
  • 825
  • 16
  • 27

2 Answers2

4

As operator in C# corresponds to TryCast in VB.NET. It returns null (Nothing) if it fails. (DirectCast throws an exception in case of failure.)

Dave Cousineau
  • 12,154
  • 8
  • 64
  • 80
Fratyx
  • 5,717
  • 1
  • 12
  • 22
2

You will need to use DirectCast Operator (Visual Basic).

If DirectCast(Parent, Popup).ProcessResizing(m) = True Then
// ....
End If
Black Frog
  • 11,595
  • 1
  • 35
  • 66
  • It is actually TryCast(). But yeah, no reason to copy a bug :) – Hans Passant Oct 13 '14 at 19:55
  • [TryCast is nice when using LINQ](http://stackoverflow.com/questions/1297297/differences-between-vb-trycast-and-c-sharp-as-operator-when-using-linq). According to [Jon Skeet's answer](http://stackoverflow.com/questions/385714/why-use-trycast-instead-of-directcast), _If the value really should be a T, then DirectCast is indeed the right way to go..._ – Black Frog Oct 13 '14 at 20:09
  • But `DirectCast` will throw if the cast is not valid whereas `TryCast` will just return `Nothing`. Of course, either way, there will be an exception. Pick your poison, I suppose. – Chris Dunaway Oct 13 '14 at 21:10
  • This is a decent answer to another question, but not the original question - why is it marked as the answer? (Fratyx's answer is the correct answer). – Dave Doknjas Oct 14 '14 at 03:46