casting question

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

    casting question

    i just can't figure out why something im doing is not
    working correctly....

    public interface IInterface
    {
    int someProperty
    {
    get;
    set;
    }
    }

    public abstract class II_interface : IInterface
    {
    private int m_someProperty;

    public int someProperty
    {
    get
    {
    return m_someProperty;
    }
    set
    {
    m_someProperty = value;
    }
    }
    }

    public class myDerivedClass : II_interface
    {
    private int m_someOtherProp erty;

    public int SomeOtherProper ty
    {
    get
    {
    return m_someOtherProp erty;
    }
    set
    {
    m_someOtherProp erty = value;
    }
    }

    public int SomeImplemented Property
    {
    get
    {
    return base.SomeProper ty*2;
    }
    }
    }

    public class myDerivedClass2 : II_interface
    {
    private int m_someOtherProp erty2;

    public int SomeOtherProper ty2
    {
    get
    {
    return m_someOtherProp erty2;
    }
    set
    {
    m_someOtherProp erty2 = value;
    }
    }
    }

    public class Test
    {
    myDerivedClass c1 = new myDerivedClass( );
    myDerivedClass2 c2 = (myDerivedClass 2)c1; //
    EXCEPTION: Specified cast is not valid.

    IInterface c3 = new myDerivedClass( );
    myDerivedClass2 c4 = (myDerivedClass )c3;//
    invalid cast


    }


    my question is why am i getting the invalid cast
    exception. If this is not possible, how can i make it
    possible.
    What i'm trying to do, is
    1) make one class with properties.
    2) derive several other class from the first class, each
    class having it's own set of properties (and since im
    deriving, all the properties from the first class)

    3) create an array of the first class
    4) for each element in array, based on a unigue type or
    enum, cast the element to the correct derived class.

    This kinka like a factory pattern, but i didn't want to
    implement the factory.. i just want to be able to cast,
    so I can set up the first classes' properties, then use
    the derived classes to access the already setup variables
    in the first class. the first class will be the only
    class with a public contructor. The rest of the derived
    classes will be casted, to use the base classes already
    created/filled properties.

    Thanks
    Kurt Lange


  • Muscha

    #2
    Re: casting question

    >[color=blue]
    > public class Test
    > {
    > myDerivedClass c1 = new myDerivedClass( );[/color]
    [color=blue]
    > my question is why am i getting the invalid cast
    > exception.[/color]

    You can't because althought myDerivedClass and myDerivedClass2 implements
    the same interface, they are not the same with each other. myDerivedClass2
    is *not* myDerivedClass. myDerivedClass2 is II_interface.

    Why do you want to cast on the list into different types? Can't you just
    store the correct object in the list? For example:

    IInterface[] array = new IInterface[] {new myDerivedClass1 (), new
    myDerivedClass2 (), new myDerivedClass3 ()};

    You can also substitute IInterface with a BaseClass.

    HTH,

    /m


    [color=blue]
    > If this is not possible, how can i make it
    > possible.
    > What i'm trying to do, is
    > 1) make one class with properties.
    > 2) derive several other class from the first class, each
    > class having it's own set of properties (and since im
    > deriving, all the properties from the first class)
    >
    > 3) create an array of the first class
    > 4) for each element in array, based on a unigue type or
    > enum, cast the element to the correct derived class.
    >
    > This kinka like a factory pattern, but i didn't want to
    > implement the factory.. i just want to be able to cast,
    > so I can set up the first classes' properties, then use
    > the derived classes to access the already setup variables
    > in the first class. the first class will be the only
    > class with a public contructor. The rest of the derived
    > classes will be casted, to use the base classes already
    > created/filled properties.
    >
    > Thanks
    > Kurt Lange
    >
    >[/color]


    Comment

    • Kurt Lange

      #3
      Re: casting question

      no... the array is created dynamically.
      and no... that defeats the purpose of what im trying todo..

      encapsulate all initializing of variables in base class...
      derive from it... by deriving from base class, and
      casting, derived classes would already have their
      variables initialiezed(ca use they have already been
      initialized in the base class) ....

      abstract class II_interface
      {
      private m_var;

      public int varVar
      {
      get
      { return m_var; }
      set
      { m_var = value; }
      }
      }

      class myDerived : II_derived
      {
      public myDerived(int val)
      { base.varVar = val }

      public int varVarVar
      {
      get { return base.varVar; }
      }
      }

      class myDerived2 : II_derived
      {
      public int varVarVar
      {
      get { return base.varVar; }
      }
      }

      class test
      {
      int function
      {
      II_interface c1 = new myDerived(6);
      II_interface c2 = (myDerived2)c1;

      // this would print "6" if this function were called
      Console.WriteLi ne("{0}",c2.var VarVar.ToString ());
      }
      }

      Notice how im initializing in the first class... and
      gather the values int he second class, without using the
      constructor on the second class.. im simply casting.

      This way im doing the work in the first class,
      initializing variables, calling the contructor, etc.

      Then by casting, all the work is done for me.. and the
      second class can use the vars in first class that are
      already initialized.

      Thanks
      [color=blue]
      >-----Original Message-----[color=green]
      >>
      >> public class Test
      >> {
      >> myDerivedClass c1 = new myDerivedClass( );[/color]
      >[color=green]
      >> my question is why am i getting the invalid cast
      >> exception.[/color]
      >
      >You can't because althought myDerivedClass and[/color]
      myDerivedClass2 implements[color=blue]
      >the same interface, they are not the same with each[/color]
      other. myDerivedClass2[color=blue]
      >is *not* myDerivedClass. myDerivedClass2 is II_interface.
      >
      >Why do you want to cast on the list into different types?[/color]
      Can't you just[color=blue]
      >store the correct object in the list? For example:
      >
      >IInterface[] array = new IInterface[] {new myDerivedClass1[/color]
      (), new[color=blue]
      >myDerivedClass 2(), new myDerivedClass3 ()};
      >
      >You can also substitute IInterface with a BaseClass.
      >
      >HTH,
      >
      >/m
      >
      >
      >[color=green]
      >> If this is not possible, how can i make it
      >> possible.
      >> What i'm trying to do, is
      >> 1) make one class with properties.
      >> 2) derive several other class from the first class, each
      >> class having it's own set of properties (and since im
      >> deriving, all the properties from the first class)
      >>
      >> 3) create an array of the first class
      >> 4) for each element in array, based on a unigue type or
      >> enum, cast the element to the correct derived class.
      >>
      >> This kinka like a factory pattern, but i didn't want to
      >> implement the factory.. i just want to be able to cast,
      >> so I can set up the first classes' properties, then use
      >> the derived classes to access the already setup[/color][/color]
      variables[color=blue][color=green]
      >> in the first class. the first class will be the only
      >> class with a public contructor. The rest of the derived
      >> classes will be casted, to use the base classes already
      >> created/filled properties.
      >>
      >> Thanks
      >> Kurt Lange
      >>
      >>[/color]
      >
      >
      >.
      >[/color]

      Comment

      • Muscha

        #4
        Re: casting question

        > no... the array is created dynamically.[color=blue]
        > and no... that defeats the purpose of what im trying todo..
        >
        > encapsulate all initializing of variables in base class...
        > derive from it... by deriving from base class, and
        > casting, derived classes would already have their
        > variables initialiezed(ca use they have already been
        > initialized in the base class) ....[/color]


        You can't do that. What you are trying to do is 'changing' the class type.
        Casting is not to change the object type. Consider this class diagram:

        A
        ^
        |--------|
        B C
        ^
        |------------|
        D E


        D d = new D();
        A a = (D) d; ///// OK

        BUT

        A a = new A();
        D d = (A) a /// NOT!

        Think of inheritance is an "IS A" problem. D IS A, but A IS NOT B. So you
        can't cast A to B. Similary in your problem what you are trying to do is
        this:

        A a = new B() // OK because B IS A
        A a2 = new C() // OK because C is A

        A a3 = (C) a // NOT ok because B IS NOT C

        What you need to do is to create a constructor in C that takes B to do the
        work for you. For example:

        A a = new B();
        A a2 = new C(a);


        HTH,

        /m
        [color=blue]
        >
        > abstract class II_interface
        > {
        > private m_var;
        >
        > public int varVar
        > {
        > get
        > { return m_var; }
        > set
        > { m_var = value; }
        > }
        > }
        >
        > class myDerived : II_derived
        > {
        > public myDerived(int val)
        > { base.varVar = val }
        >
        > public int varVarVar
        > {
        > get { return base.varVar; }
        > }
        > }
        >
        > class myDerived2 : II_derived
        > {
        > public int varVarVar
        > {
        > get { return base.varVar; }
        > }
        > }
        >
        > class test
        > {
        > int function
        > {
        > II_interface c1 = new myDerived(6);
        > II_interface c2 = (myDerived2)c1;
        >
        > // this would print "6" if this function were called
        > Console.WriteLi ne("{0}",c2.var VarVar.ToString ());
        > }
        > }
        >
        > Notice how im initializing in the first class... and
        > gather the values int he second class, without using the
        > constructor on the second class.. im simply casting.
        >
        > This way im doing the work in the first class,
        > initializing variables, calling the contructor, etc.
        >
        > Then by casting, all the work is done for me.. and the
        > second class can use the vars in first class that are
        > already initialized.
        >
        > Thanks
        >[color=green]
        > >-----Original Message-----[color=darkred]
        > >>
        > >> public class Test
        > >> {
        > >> myDerivedClass c1 = new myDerivedClass( );[/color]
        > >[color=darkred]
        > >> my question is why am i getting the invalid cast
        > >> exception.[/color]
        > >
        > >You can't because althought myDerivedClass and[/color]
        > myDerivedClass2 implements[color=green]
        > >the same interface, they are not the same with each[/color]
        > other. myDerivedClass2[color=green]
        > >is *not* myDerivedClass. myDerivedClass2 is II_interface.
        > >
        > >Why do you want to cast on the list into different types?[/color]
        > Can't you just[color=green]
        > >store the correct object in the list? For example:
        > >
        > >IInterface[] array = new IInterface[] {new myDerivedClass1[/color]
        > (), new[color=green]
        > >myDerivedClass 2(), new myDerivedClass3 ()};
        > >
        > >You can also substitute IInterface with a BaseClass.
        > >
        > >HTH,
        > >
        > >/m
        > >
        > >
        > >[color=darkred]
        > >> If this is not possible, how can i make it
        > >> possible.
        > >> What i'm trying to do, is
        > >> 1) make one class with properties.
        > >> 2) derive several other class from the first class, each
        > >> class having it's own set of properties (and since im
        > >> deriving, all the properties from the first class)
        > >>
        > >> 3) create an array of the first class
        > >> 4) for each element in array, based on a unigue type or
        > >> enum, cast the element to the correct derived class.
        > >>
        > >> This kinka like a factory pattern, but i didn't want to
        > >> implement the factory.. i just want to be able to cast,
        > >> so I can set up the first classes' properties, then use
        > >> the derived classes to access the already setup[/color][/color]
        > variables[color=green][color=darkred]
        > >> in the first class. the first class will be the only
        > >> class with a public contructor. The rest of the derived
        > >> classes will be casted, to use the base classes already
        > >> created/filled properties.
        > >>
        > >> Thanks
        > >> Kurt Lange
        > >>
        > >>[/color]
        > >
        > >
        > >.
        > >[/color][/color]


        Comment

        Working...