Calling non-static methods from within static methods.

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

    #1

    Calling non-static methods from within static methods.

    Is the following legal/recommended? (It compiles w/o error or
    warnings)

    class MyClass()
    {
    public MyClass()
    {
    }

    private string NonStaticMethod ()
    {
    retrun "Something" ;
    }

    private static string StaticMethod()
    {
    MyClass mc = new MyClass();

    return mc.NonStaticMet hod();
    }
    }

  • AlexS

    #2
    Re: Calling non-static methods from within static methods.

    Yes. In short.

    You should consider that MyClass might need proper Dispose after use. If you
    will adhere to recommended practices.

    However, "retrun" shouldn't compile. You don't get any errors?


    "koredump" <koredump@inter ia.plwrote in message
    news:1184772713 .024803.326360@ o11g2000prd.goo glegroups.com.. .
    Is the following legal/recommended? (It compiles w/o error or
    warnings)
    >
    class MyClass()
    {
    public MyClass()
    {
    }
    >
    private string NonStaticMethod ()
    {
    retrun "Something" ;
    }
    >
    private static string StaticMethod()
    {
    MyClass mc = new MyClass();
    >
    return mc.NonStaticMet hod();
    }
    }
    >

    Comment

    • =?Utf-8?B?QWRyaWFuIFZvaWN1?=

      #3
      RE: Calling non-static methods from within static methods.


      What you have looks similar to the Singleton design pattern where you have a
      static method that everyone can call without having to create any objects.
      This in turn returns a single existing instance of a resource that is shared
      by everyone. Apart from that I don't see any other reason for you to make a
      static method which instantiates the class it is in. You can read more about
      the Singleton design pattern here:

      Learn how to use the C# Singleton design pattern to ensure that only one instance of a class is ever created, with quick and easy examples. 100% Source code.


      Adrian.
      --
      [Please mark my answer if it was helpful to you]





      "koredump" wrote:
      Is the following legal/recommended? (It compiles w/o error or
      warnings)
      >
      class MyClass()
      {
      public MyClass()
      {
      }
      >
      private string NonStaticMethod ()
      {
      retrun "Something" ;
      }
      >
      private static string StaticMethod()
      {
      MyClass mc = new MyClass();
      >
      return mc.NonStaticMet hod();
      }
      }
      >
      >

      Comment

      • Ben Voigt [C++ MVP]

        #4
        Re: Calling non-static methods from within static methods.


        "koredump" <koredump@inter ia.plwrote in message
        news:1184772713 .024803.326360@ o11g2000prd.goo glegroups.com.. .
        Is the following legal/recommended? (It compiles w/o error or
        warnings)
        Certainly legal. The only thing I see that is suspicious is locally
        creating an instance to use, as typically the instance(s) will be passed in
        via parameter. Operator overloads are required to work in this manner.
        >
        class MyClass()
        {
        public MyClass()
        {
        }
        >
        private string NonStaticMethod ()
        {
        retrun "Something" ;
        }
        >
        private static string StaticMethod()
        {
        MyClass mc = new MyClass();
        >
        return mc.NonStaticMet hod();
        }
        }
        >

        Comment

        Working...