Casting

Discussion in 'ASP .Net' started by =?Utf-8?B?Sm9u?=, Feb 21, 2007.

  1. Hello all,

    I have an object of type B that inherits from an object of type A.

    I create a new instace of B, but I need to cast/convert it to an object of
    type A in order to call a particular method. I've tried all sorts of ways to
    cast it as A but it always stays as an object of type B. The below is an
    example of the code I've tried:

    A a = B.Create();
    A a = b as A;

    No matter what I do, it always stays as B!!

    Can anyone offer any help?

    Thanks,

    Jon
     
    =?Utf-8?B?Sm9u?=, Feb 21, 2007
    #1
    1. Advertising

  2. Actually it is base.MethodName if you want to call parent class's method in
    derived class.you will find following link useful:
    http://msdn.microsoft.com/msdnmag/issues/01/12/instincts/

    "Jon" wrote:

    > Hello all,
    >
    > I have an object of type B that inherits from an object of type A.
    >
    > I create a new instace of B, but I need to cast/convert it to an object of
    > type A in order to call a particular method. I've tried all sorts of ways to
    > cast it as A but it always stays as an object of type B. The below is an
    > example of the code I've tried:
    >
    > A a = B.Create();
    > A a = b as A;
    >
    > No matter what I do, it always stays as B!!
    >
    > Can anyone offer any help?
    >
    > Thanks,
    >
    > Jon
     
    =?Utf-8?B?TWFuaXNoIEJhZm5h?=, Feb 21, 2007
    #2
    1. Advertising

  3. Hi,

    the method in A is virtual and overridden in B, but i need to call the A
    method. How?

    Jon

    "Manish Bafna" wrote:

    > Hi,
    > Obviously it will remain B only.If you want to call some method of A Class
    > then make that method virtual.Then that method will be available in B Class
    > also.
    > Hope this helps you out.
    >
    > Thanks and regards,
    > Manish Bafna.
    > MCP and MCTS.
    >
    > "Jon" wrote:
    >
    > > Hello all,
    > >
    > > I have an object of type B that inherits from an object of type A.
    > >
    > > I create a new instace of B, but I need to cast/convert it to an object of
    > > type A in order to call a particular method. I've tried all sorts of ways to
    > > cast it as A but it always stays as an object of type B. The below is an
    > > example of the code I've tried:
    > >
    > > A a = B.Create();
    > > A a = b as A;
    > >
    > > No matter what I do, it always stays as B!!
    > >
    > > Can anyone offer any help?
    > >
    > > Thanks,
    > >
    > > Jon
     
    =?Utf-8?B?Sm9u?=, Feb 21, 2007
    #3
  4. If you want to call method present in A,then dont make it virtual instead
    make it protected then it will be available in B class also


    "Jon" wrote:

    > Hi,
    >
    > the method in A is virtual and overridden in B, but i need to call the A
    > method. How?
    >
    > Jon
    >
    > "Manish Bafna" wrote:
    >
    > > Hi,
    > > Obviously it will remain B only.If you want to call some method of A Class
    > > then make that method virtual.Then that method will be available in B Class
    > > also.
    > > Hope this helps you out.
    > >
    > > Thanks and regards,
    > > Manish Bafna.
    > > MCP and MCTS.
    > >
    > > "Jon" wrote:
    > >
    > > > Hello all,
    > > >
    > > > I have an object of type B that inherits from an object of type A.
    > > >
    > > > I create a new instace of B, but I need to cast/convert it to an object of
    > > > type A in order to call a particular method. I've tried all sorts of ways to
    > > > cast it as A but it always stays as an object of type B. The below is an
    > > > example of the code I've tried:
    > > >
    > > > A a = B.Create();
    > > > A a = b as A;
    > > >
    > > > No matter what I do, it always stays as B!!
    > > >
    > > > Can anyone offer any help?
    > > >
    > > > Thanks,
    > > >
    > > > Jon
     
    =?Utf-8?B?TWFuaXNoIEJhZm5h?=, Feb 21, 2007
    #4
  5. When calling a virtual method you are always calling the method of the
    actual class. If you don't want it that way, then you shouldn't make it
    virtual.

    Jon wrote:
    > Hi,
    >
    > the method in A is virtual and overridden in B, but i need to call the A
    > method. How?
    >
    > Jon
    >
    > "Manish Bafna" wrote:
    >
    >> Hi,
    >> Obviously it will remain B only.If you want to call some method of A Class
    >> then make that method virtual.Then that method will be available in B Class
    >> also.
    >> Hope this helps you out.
    >>
    >> Thanks and regards,
    >> Manish Bafna.
    >> MCP and MCTS.
    >>
    >> "Jon" wrote:
    >>
    >>> Hello all,
    >>>
    >>> I have an object of type B that inherits from an object of type A.
    >>>
    >>> I create a new instace of B, but I need to cast/convert it to an object of
    >>> type A in order to call a particular method. I've tried all sorts of ways to
    >>> cast it as A but it always stays as an object of type B. The below is an
    >>> example of the code I've tried:
    >>>
    >>> A a = B.Create();
    >>> A a = b as A;
    >>>
    >>> No matter what I do, it always stays as B!!
    >>>
    >>> Can anyone offer any help?
    >>>
    >>> Thanks,
    >>>
    >>> Jon



    --
    Göran Andersson
    _____
    http://www.guffa.com
     
    =?UTF-8?B?R8O2cmFuIEFuZGVyc3Nvbg==?=, Feb 21, 2007
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ben Nguyen
    Replies:
    6
    Views:
    18,895
  2. Toby
    Replies:
    3
    Views:
    3,101
    Mike Treseler
    Sep 7, 2005
  3. kevin
    Replies:
    11
    Views:
    5,816
    Andrew McDonagh
    Jan 8, 2005
  4. Wally Barnes
    Replies:
    3
    Views:
    533
    Wally Barnes
    Nov 20, 2008
  5. Sosuke

    Up casting and down casting

    Sosuke, Dec 20, 2009, in forum: C++
    Replies:
    2
    Views:
    575
    James Kanze
    Dec 20, 2009
Loading...

Share This Page