Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. void and static void problem

void and static void problem

Scheduled Pinned Locked Moved C#
helptutorial
5 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    Rick van Woudenberg
    wrote on last edited by
    #1

    Dear all, is is possible to call a static method from a non-static method and the other way around eg.:

    private void Test1()
    {
    Test2();
    }

    private static void Test2()
    {
    //some stuff here
    }

    The example above will generate an error, but I would like to know if , and how it can be done. Thank you for any assistance. Kind regards,

    G J L 3 Replies Last reply
    0
    • R Rick van Woudenberg

      Dear all, is is possible to call a static method from a non-static method and the other way around eg.:

      private void Test1()
      {
      Test2();
      }

      private static void Test2()
      {
      //some stuff here
      }

      The example above will generate an error, but I would like to know if , and how it can be done. Thank you for any assistance. Kind regards,

      G Offline
      G Offline
      Giorgi Dalakishvili
      wrote on last edited by
      #2

      Do you know how to call static methods from outside the class where it is defined? You can call it in the same way. If not then it means that you are not familiar with static methods so why do you use it? As for second question you will need an instance of a class that defines nonstatic method.

      Giorgi Dalakishvili #region signature my articles My blog[^] #endregion

      1 Reply Last reply
      0
      • R Rick van Woudenberg

        Dear all, is is possible to call a static method from a non-static method and the other way around eg.:

        private void Test1()
        {
        Test2();
        }

        private static void Test2()
        {
        //some stuff here
        }

        The example above will generate an error, but I would like to know if , and how it can be done. Thank you for any assistance. Kind regards,

        J Offline
        J Offline
        J4amieC
        wrote on last edited by
        #3

        Within the same class, an instance method can call a static, but a static cannot call an instance.

        S 1 Reply Last reply
        0
        • J J4amieC

          Within the same class, an instance method can call a static, but a static cannot call an instance.

          S Offline
          S Offline
          Spacix One
          wrote on last edited by
          #4

          unless you pass an instance reference to it...


          -Spacix All your skynet questions[^] belong to solved


          I dislike the black-and-white voting system on questions/answers. X|


          1 Reply Last reply
          0
          • R Rick van Woudenberg

            Dear all, is is possible to call a static method from a non-static method and the other way around eg.:

            private void Test1()
            {
            Test2();
            }

            private static void Test2()
            {
            //some stuff here
            }

            The example above will generate an error, but I would like to know if , and how it can be done. Thank you for any assistance. Kind regards,

            L Offline
            L Offline
            Le centriste
            wrote on last edited by
            #5

            The only way to do this is to have your Test2 method have an instance of the class as a parameter or make one available to it in some other way.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups