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. newbie question

newbie question

Scheduled Pinned Locked Moved C#
questioncsharpc++visual-studiocom
8 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.
  • T Offline
    T Offline
    Tom Wright
    wrote on last edited by
    #1

    I VC++ if you creaed a function to do a specific thing you first referenced it in the header file then added to the cpp file. Example: Header: int myFunc(); CPP: int CMyClassDLG::myFinc() { } So in C# I created this dialog based app. I added a function to get all the nic listed on my machine. Called the function GetNics(). How do I call this? When I try and call this in Main() is says something about "C:\My Documents\Visual Studio Projects\MyNetwork\Form1.cs(111): An object reference is required for the nonstatic field, method, or property 'MyNetwork.Form1.GetNics()'" So do I need to create a new class for each new function I want to do? Can someone point me in the right direction? Thanks Tom Wright tawright915@yahoo.com

    T U 2 Replies Last reply
    0
    • T Tom Wright

      I VC++ if you creaed a function to do a specific thing you first referenced it in the header file then added to the cpp file. Example: Header: int myFunc(); CPP: int CMyClassDLG::myFinc() { } So in C# I created this dialog based app. I added a function to get all the nic listed on my machine. Called the function GetNics(). How do I call this? When I try and call this in Main() is says something about "C:\My Documents\Visual Studio Projects\MyNetwork\Form1.cs(111): An object reference is required for the nonstatic field, method, or property 'MyNetwork.Form1.GetNics()'" So do I need to create a new class for each new function I want to do? Can someone point me in the right direction? Thanks Tom Wright tawright915@yahoo.com

      T Offline
      T Offline
      Tom Wright
      wrote on last edited by
      #2

      Okay I think I may have found it. Can I not call any functions in the Main portion of my code? What I was doing was trying to load up a dropdown list when the form first loaded. I found out that by double clicking on the form created a function call Form1_Load in whick I could call my functions. So is this how I am supposed to do it? Tom Wright tawright915@yahoo.com

      K 1 Reply Last reply
      0
      • T Tom Wright

        Okay I think I may have found it. Can I not call any functions in the Main portion of my code? What I was doing was trying to load up a dropdown list when the form first loaded. I found out that by double clicking on the form created a function call Form1_Load in whick I could call my functions. So is this how I am supposed to do it? Tom Wright tawright915@yahoo.com

        K Offline
        K Offline
        Kevin McFarlane
        wrote on last edited by
        #3

        In the Main function you can only call other static functions in that class. Otherwise you must put your functions in a separate class and then new the class in Main and then call the function on that class. Kevin

        1 Reply Last reply
        0
        • T Tom Wright

          I VC++ if you creaed a function to do a specific thing you first referenced it in the header file then added to the cpp file. Example: Header: int myFunc(); CPP: int CMyClassDLG::myFinc() { } So in C# I created this dialog based app. I added a function to get all the nic listed on my machine. Called the function GetNics(). How do I call this? When I try and call this in Main() is says something about "C:\My Documents\Visual Studio Projects\MyNetwork\Form1.cs(111): An object reference is required for the nonstatic field, method, or property 'MyNetwork.Form1.GetNics()'" So do I need to create a new class for each new function I want to do? Can someone point me in the right direction? Thanks Tom Wright tawright915@yahoo.com

          U Offline
          U Offline
          Utku KAYA
          wrote on last edited by
          #4

          If you want to call the GetNics() method in the class it beongs to, you just call it as GetNics() , if you call it outside the class you have to create an instance of the owner class and then call it as 'instanceNameHere.GetNics()' For only static methods you can use ClassNameHere.MethodNameHere() Good luck.

          T 1 Reply Last reply
          0
          • U Utku KAYA

            If you want to call the GetNics() method in the class it beongs to, you just call it as GetNics() , if you call it outside the class you have to create an instance of the owner class and then call it as 'instanceNameHere.GetNics()' For only static methods you can use ClassNameHere.MethodNameHere() Good luck.

            T Offline
            T Offline
            Tom Wright
            wrote on last edited by
            #5

            I'm not sure what you mean by this, Utku KAYA wrote: 'instanceNameHere.GetNics()' Here is my code: static void Main() { Application.Run(new Form1()); MyNetwork.LoadNics(); } private void Form1_Load(object sender, System.EventArgs e) { LoadNics(); } private void LoadNics() { ArrayList nicName = WMIHelper.GetNics(); NICScb.Items.Clear(); foreach ( String name in nicName) { NICScb.Items.Add(name); } if (NICScb.Items.Count > 0) { NICScb.SelectedIndex = 0; } } } I still get an error in the main function. I read that the main function does not belong to the object, so I see what you mean by referencing the instance. But it still errors out. Can you explain some more for me? Thanks Tom Wright tawright915@yahoo.com

            P 1 Reply Last reply
            0
            • T Tom Wright

              I'm not sure what you mean by this, Utku KAYA wrote: 'instanceNameHere.GetNics()' Here is my code: static void Main() { Application.Run(new Form1()); MyNetwork.LoadNics(); } private void Form1_Load(object sender, System.EventArgs e) { LoadNics(); } private void LoadNics() { ArrayList nicName = WMIHelper.GetNics(); NICScb.Items.Clear(); foreach ( String name in nicName) { NICScb.Items.Add(name); } if (NICScb.Items.Count > 0) { NICScb.SelectedIndex = 0; } } } I still get an error in the main function. I read that the main function does not belong to the object, so I see what you mean by referencing the instance. But it still errors out. Can you explain some more for me? Thanks Tom Wright tawright915@yahoo.com

              P Offline
              P Offline
              Polis Pilavas
              wrote on last edited by
              #6

              By placing the LoadNics() method into the FormLoad event, it means that every time the forms get created (or, if u prefer, the class gets intantiated) the LoadNics() method will be executed. Just cut & paste the LoadNics() method call from the FormLoad event into the Main() method of your class. So your new Main() class would consist something like:

              static void Main()
              {
              Application.Run(new Form1());
              LoadNics(); // or this.LoadNics();

              }

              Regards, Polis Can you practice what you teach?

              L T 2 Replies Last reply
              0
              • P Polis Pilavas

                By placing the LoadNics() method into the FormLoad event, it means that every time the forms get created (or, if u prefer, the class gets intantiated) the LoadNics() method will be executed. Just cut & paste the LoadNics() method call from the FormLoad event into the Main() method of your class. So your new Main() class would consist something like:

                static void Main()
                {
                Application.Run(new Form1());
                LoadNics(); // or this.LoadNics();

                }

                Regards, Polis Can you practice what you teach?

                L Offline
                L Offline
                leppie
                wrote on last edited by
                #7

                Polis Pilavas wrote: Application.Run(new Form1()); LoadNics(); // or this.LoadNics(); LoadNics() will only be called when the application exits. Using 'this' in a static method is also very suspect ;P xacc-ide 0.0.15 now with C#, MSIL, C, XML, ASP.NET, Nemerle, MyXaml and HLSL coloring - Screenshots

                1 Reply Last reply
                0
                • P Polis Pilavas

                  By placing the LoadNics() method into the FormLoad event, it means that every time the forms get created (or, if u prefer, the class gets intantiated) the LoadNics() method will be executed. Just cut & paste the LoadNics() method call from the FormLoad event into the Main() method of your class. So your new Main() class would consist something like:

                  static void Main()
                  {
                  Application.Run(new Form1());
                  LoadNics(); // or this.LoadNics();

                  }

                  Regards, Polis Can you practice what you teach?

                  T Offline
                  T Offline
                  Tom Wright
                  wrote on last edited by
                  #8

                  That does not work. I've tried it several times. The problem is that while Main belongs to the class rather than any particular object of the class, my LodNic() function belongs to a particular object of the class...this is why it is not a static function, and why it errors out. Tom Wright tawright915@yahoo.com

                  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