0

here is a snippet of code:

    Dim gotoAction As PDFGoToAction = New PDFGoToAction()
        ' //PDF4NET 5: gotoAction.Destination = new PDFPageDestination();
        gotoAction.Destination = New PDFPageDirectDestination()
        '//PDF4NET 5: gotoAction.Destination.Page = destinationPage;
        (gotoAction.Destination as PDFPageDirectDestination).Page = destinationPage;

The problem I have is with the following line:

(gotoAction.Destination as PDFPageDirectDestination).Page = destinationPage;

In C# casting this object in this manner is allowed but in Visual Basic it is not. I've been searching online for examples and couldn't find the visual basic version of the statement above.

rioV8
  • 24,506
  • 3
  • 32
  • 49
  • sorry, yes. you're right! – Mr Ambitious Mar 11 '21 at 14:22
  • 1
    You're looking for `TryCast`, and VB doesn't use semicolon terminators. By the way, you can avoid that by writing `Dim gotoAction As PDFGoToAction = New PDFGoToAction() With { Destination = New PDFPageDirectDestination() With {Page = destinationPage} }` – Charlieface Mar 11 '21 at 14:24
  • Does this answer your question? [VB.NET equivalent of C# "As"](https://stackoverflow.com/questions/2450185/vb-net-equivalent-of-c-sharp-as) – Charlieface Mar 11 '21 at 14:24
  • https://converter.telerik.com/ suggests `(TryCast(gotoAction.Destination, PDFPageDirectDestination)).Page = destinationPage` . There are couple of these automatic converters around, they will translate most things without big issues. – ADyson Mar 11 '21 at 14:27
  • 3
    I suggest `DirectCast(gotoAction.Destination, PDFPageDirectDestination).Page = destinationPage`, since `gotoAction.Destination` is clearly a `PDFPageDirectDestination` object. Not clear why you cannot just assign `gotoAction.Destination.Page = destinationPage`: you created a new `gotoAction.Destination` object of type `PDFPageDirectDestination` one line before this one. – Jimi Mar 11 '21 at 14:29
  • 1
    While `TryCast` is a direct translation, it's a bad choice for this usage (though to be fair, it's a bad choice in C# as well). When it goes wrong, it gives a misleading NullReferenceException instead of (if done correctly using a direct cast) an InvalidCastException. The correct way to write this line in C# is `((PDFPageDirectionDestination)gotoAction.Destination).Page = destinationPage;`. `as` in C# and `TryCast` in VB should only be used if the result will be checked for `null` or `Nothing`. – Craig Mar 11 '21 at 21:00
  • The other VB alternative that could be used is `CType`. This will also look for potential conversions, which won't always be what you want to do. – Craig Mar 11 '21 at 21:03

1 Answers1

0

i think what you are looking for is DirectCast, your code could be something like this:

DirectCast(gotoAction.Destination,PDFPageDirectDestination).page = destinationPage

here is more info about direct cast:

https://learn.microsoft.com/en-us/dotnet/visual-basic/language-reference/operators/directcast-operator

MorenajeRD
  • 849
  • 1
  • 11
  • 27
  • As Charlieface pointed out in a comment, the equivalent is TryCast, not DirectCast. – Dave Doknjas Mar 11 '21 at 16:53
  • @DaveDoknjas While it's true that this isn't a 100% exact translation, `DirectCast` is the right way to do this. Also see my comment on the question. – Craig Mar 11 '21 at 21:00