static method question


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: static method question

Hybrid View

  1. #1
    Sean Woods Guest

    static method question

    Is there some way inside a static method to determine whether it is being
    called on a base class or an inherited class. For instance:

    public class BaseClass
    {
    public BaseClass() {}

    public static MyMethod()
    {
    }

    // other non-static methods
    }

    public class InheritedClass : BaseClass
    {
    public InheritedClass() {}

    //notice this one doesn't implement or override MyMethod
    }

    So is this there a way to determine inside of MyMethod whether it was called
    via BaseClass.MyMethod or InheritedClass.MyMethod?

    If it wasn't static I could use "this" to get its type. So how do I do it
    since it is static?

    Thanks
    Sean



  2. #2
    Jeff Johnson Guest

    Re: static method question


    "Sean Woods" <swoods@eclickmd.com> wrote:
    >Is there some way inside a static method to determine whether it is being
    >called on a base class or an inherited class.
    >
    >So is this there a way to determine inside of MyMethod whether it was called
    >via BaseClass.MyMethod or InheritedClass.MyMethod?
    >
    >If it wasn't static I could use "this" to get its type. So how do I do

    it
    >since it is static?
    >
    >Thanks
    >Sean



    If you use nonstatic methods, you can use this.GetType() to find out what
    sort of class you're in.

    ... Or you can override a virtual method and have different behavior based
    on the object type.

    But you can't know what type of "class" you're in from a static non-virtual
    method.

    (Unless I'm wrong!)

    -- Jeff

  3. #3
    Partha Guest

    Re: static method question

    As far as my knowledge goes, you cannot call a static method using the
    object reference. You have to use the class name to do so. In these
    scenario, does it really matter whether I am calling the function of the
    base or the derived class? I know the type before making the call.


    "Jeff Johnson" <jeff@nospam.com> wrote in message
    news:3e4d1d22$1@tnews.web.devx.com...
    >
    > "Sean Woods" <swoods@eclickmd.com> wrote:
    > >Is there some way inside a static method to determine whether it is being
    > >called on a base class or an inherited class.
    > >
    > >So is this there a way to determine inside of MyMethod whether it was

    called
    > >via BaseClass.MyMethod or InheritedClass.MyMethod?
    > >
    > >If it wasn't static I could use "this" to get its type. So how do I do

    > it
    > >since it is static?
    > >
    > >Thanks
    > >Sean

    >
    >
    > If you use nonstatic methods, you can use this.GetType() to find out what
    > sort of class you're in.
    >
    > .. Or you can override a virtual method and have different behavior based
    > on the object type.
    >
    > But you can't know what type of "class" you're in from a static

    non-virtual
    > method.
    >
    > (Unless I'm wrong!)
    >
    > -- Jeff




Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center