alternative for TryCast

Posted by DenDuze on 13-Jan-2017 01:51

I need to transform some .net code and there they have used a TryCast operator (https://docs.microsoft.com/en-us/dotnet/articles/visual-basic/language-reference/operators/trycast-operator)
Is there some alternative that we can use in OpenEdge?

Posted by Mike Fechner on 13-Jan-2017 01:58

You’ll need a combination of TYPE-OF and CAST.

Posted by Simon L. Prinsloo on 13-Jan-2017 02:07

DEFINE VARIABLE objAsConvertible AS IConvertible NO-UNDO.

IF TYPE-OF(obj, IConvertible)
THEN objAsConvertible = CAST(obj, IConvertible).

IF NOT VALID-OBJECT(objAsConvertible)
THEN RETURN obj:GetClass():TypeName + " does not implement IConvertible".
ELSE RETURN "Type code is " + objAsConvertible:GetTypeCode.

All Replies

Posted by Mike Fechner on 13-Jan-2017 01:58

You’ll need a combination of TYPE-OF and CAST.

Posted by Simon L. Prinsloo on 13-Jan-2017 02:07

DEFINE VARIABLE objAsConvertible AS IConvertible NO-UNDO.

IF TYPE-OF(obj, IConvertible)
THEN objAsConvertible = CAST(obj, IConvertible).

IF NOT VALID-OBJECT(objAsConvertible)
THEN RETURN obj:GetClass():TypeName + " does not implement IConvertible".
ELSE RETURN "Type code is " + objAsConvertible:GetTypeCode.

Posted by DenDuze on 13-Jan-2017 02:54

Hi Simon,

Tried this and it compiles but it doesn't work as expected.

Maybe the original code that I want to port to Progress is wrong or maybe I did something wrong.

I see that you do the cast with an interface. Is that needed or can it be another class.

Code that I try to port:

If TryCast(RadGrid1.CurrentRow, GridViewDataRowInfo) IsNot Nothing Then ....

This is what I have now:

def var o as Telerik.WinControls.UI.GridViewDataRowInfo no-undo.

  IF TYPE-OF(RadGridView1:CurrentRow, Telerik.WinControls.UI.GridViewDataRowInfo) then

    assign o = CAST(RadGridView1:CurrentRow, Telerik.WinControls.UI.GridViewDataRowInfo).

Is that ported ok??

Posted by DenDuze on 13-Jan-2017 06:49

Mike/Simon,

Thanks, I can use your suggestion as replacement for TryCath.

It didn't solve my problem but I guess that the code that I wanted to port is also not correct.

Found another way to accomplish what I want but still thanks for your input

regards

Didier

This thread is closed