Report post

Are you sure you want to report the post shown below? This will send an email to the ATVO administrators. Please include a short reason for reporting.

Users reporting for no reason may be locked out.


Post

Posted by Nick Thissen
on
There is only one such cast in the very beginning. My guess is you are accidentally using this script on another binding as well? Otherwise I cannot explain why it would suddenly give you a string instead of an IEntitySessionResult object. Unless there's a bug somewhere.

There is also a "mistake" that you cast value before you check if it is null. If value is null (which I can see happening in some cases) then this cast will already throw an exception (but a different one).

You can probably fix it just by checking the type of the value first. Or casting it in a different way. You have several options, as a bonus I think they all do the null check automatically:

1. Use "is" to check the type, then cast only if it is the correct type. Probably no need for null check anymore.
if (value is IEntitySessionResult)
{
    var result = (IEntitySessionResult)value;

    // continue
}
else
{
    return "";
}


2. Use "as" to do a "safe" cast: if the type is not right, this will not error but just make result null. This is basically shorthand for option 1.
var result = value as IEntitySessionResult;
if (result == null)
{
    return "";
}

// continue


3. Or maybe even this one (not sure if we support this more recent C# feature) where the type check and cast is done at the same time:
if (value is IEntitySessionResult result)
{
    // continue
}
else
{
    return "";
}