Why isn't this casting?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Joe

    #1

    Why isn't this casting?

    entityType implements IPersistent and does get into the if statement but p
    is always null.

    Type t = entityType.GetI nterface(typeof (IPersistent).N ame);

    if (t != null)
    {
    IPersistent p = entityType as IPersistent;

    // p Is always null.
    }

    -Joe


  • Bruno Jouhier

    #2
    Re: Why isn't this casting?

    You are mixing the "type" and the "instance"

    In your code, entityType is a type.
    You probably have a variable like entityInstance that references an instance
    of entityType, and the "as" operator should be applied to this variable:

    IPersistent p = entityInstance as IPersistent;

    Bruno

    "Joe" <jbassking@noem ail.noemail> a écrit dans le message de news:
    %23QohlQeOGHA.3 360@TK2MSFTNGP1 5.phx.gbl...[color=blue]
    > entityType implements IPersistent and does get into the if statement but p
    > is always null.
    >
    > Type t = entityType.GetI nterface(typeof (IPersistent).N ame);
    >
    > if (t != null)
    > {
    > IPersistent p = entityType as IPersistent;
    >
    > // p Is always null.
    > }
    >
    > -Joe
    >[/color]


    Comment

    • Joe

      #3
      Re: Why isn't this casting?

      Bruno,

      Does this mean I get the stupid question award for the year? I can't believe
      I didn't realize I was trying to cast the type and not the object.

      Thanks for your clear set of eyes!

      -Joe

      "Bruno Jouhier" <bjouhier@clu b-internet.fr> wrote in message
      news:44001767$0 $1146$7a628cd7@ news.club-internet.fr...[color=blue]
      > You are mixing the "type" and the "instance"
      >
      > In your code, entityType is a type.
      > You probably have a variable like entityInstance that references an
      > instance of entityType, and the "as" operator should be applied to this
      > variable:
      >
      > IPersistent p = entityInstance as IPersistent;
      >
      > Bruno
      >
      > "Joe" <jbassking@noem ail.noemail> a écrit dans le message de news:
      > %23QohlQeOGHA.3 360@TK2MSFTNGP1 5.phx.gbl...[color=green]
      >> entityType implements IPersistent and does get into the if statement but
      >> p is always null.
      >>
      >> Type t = entityType.GetI nterface(typeof (IPersistent).N ame);
      >>
      >> if (t != null)
      >> {
      >> IPersistent p = entityType as IPersistent;
      >>
      >> // p Is always null.
      >> }
      >>
      >> -Joe
      >>[/color]
      >
      >[/color]


      Comment

      • Bruno Jouhier

        #4
        Re: Why isn't this casting?


        "Joe" <jbassking@noem ail.noemail> a écrit dans le message de news:
        uy6rSiiOGHA.201 2@TK2MSFTNGP14. phx.gbl...[color=blue]
        > Bruno,
        >
        > Does this mean I get the stupid question award for the year?[/color]

        Probably not, unfortunately!
        [color=blue]
        > I can't believe I didn't realize I was trying to cast the type and not the
        > object.
        >
        > Thanks for your clear set of eyes![/color]

        You're very welcome.

        Bruno.
        [color=blue]
        >
        > -Joe
        >
        > "Bruno Jouhier" <bjouhier@clu b-internet.fr> wrote in message
        > news:44001767$0 $1146$7a628cd7@ news.club-internet.fr...[color=green]
        >> You are mixing the "type" and the "instance"
        >>
        >> In your code, entityType is a type.
        >> You probably have a variable like entityInstance that references an
        >> instance of entityType, and the "as" operator should be applied to this
        >> variable:
        >>
        >> IPersistent p = entityInstance as IPersistent;
        >>
        >> Bruno
        >>
        >> "Joe" <jbassking@noem ail.noemail> a écrit dans le message de news:
        >> %23QohlQeOGHA.3 360@TK2MSFTNGP1 5.phx.gbl...[color=darkred]
        >>> entityType implements IPersistent and does get into the if statement but
        >>> p is always null.
        >>>
        >>> Type t = entityType.GetI nterface(typeof (IPersistent).N ame);
        >>>
        >>> if (t != null)
        >>> {
        >>> IPersistent p = entityType as IPersistent;
        >>>
        >>> // p Is always null.
        >>> }
        >>>
        >>> -Joe
        >>>[/color]
        >>
        >>[/color]
        >
        >[/color]


        Comment

        Working...