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