Register for your free account! | Forgot your password?

Go Back   elitepvpers > MMORPGs > Conquer Online 2 > CO2 Programming
You last visited: Today at 22:53

  • Please register to post and access all features, it's quick, easy and FREE!

Advertisement



[C# Err]An object reference is required for the non-static field, method, or property

Discussion on [C# Err]An object reference is required for the non-static field, method, or property within the CO2 Programming forum part of the Conquer Online 2 category.

Reply
 
Old   #1

 
jackpotsvr's Avatar
 
elite*gold: 20
Join Date: Oct 2008
Posts: 328
Received Thanks: 43
[C# Err]An object reference is required for the non-static field, method, or property

Hey was programming some Form and I want to be able to clear its controls with this void, but it won't work. What am I doing wrong?


An object reference is required for the non-static field, method, or property
Code:
public void SwitchUC()
        {
            Form1.Controls.Clear();
        }
jackpotsvr is offline  
Old 01/26/2012, 16:17   #2
 
donn's Avatar
 
elite*gold: 0
Join Date: Jan 2007
Posts: 485
Received Thanks: 272
Everything. You can't clear all controls from a form with one line.
You need to:
  • Remove the event handler from the event
  • Use Remove method to delete the desired control
  • Call the Dispose method to release all the resources used by the control.
And this for each individual control you want to remove (you can forloop them).
donn is offline  
Thanks
1 User
Old 01/26/2012, 16:19   #3

 
jackpotsvr's Avatar
 
elite*gold: 20
Join Date: Oct 2008
Posts: 328
Received Thanks: 43
Quote:
Originally Posted by donn View Post
Everything. You can't clear all controls from a form with one line.
You need to:
  • Remove the event handler from the event
  • Use Remove method to delete the desired control
  • Call the Dispose method to release all the resources used by the control.
And this for each individual control you want to remove (you can forloop them).
The line of code did work for aslong I had it on a button, but now it's in a void it does not :/
jackpotsvr is offline  
Old 01/26/2012, 16:27   #4
 
donn's Avatar
 
elite*gold: 0
Join Date: Jan 2007
Posts: 485
Received Thanks: 272
Quote:
Originally Posted by jackpotsvr View Post
The line of code did work for aslong I had it on a button, but now it's in a void it does not :/
It worked because a button is an object reference. Still, without removing any event handler associated with a control and disposing the resources used by it, you will leave a lot of dirt behind.
donn is offline  
Thanks
1 User
Old 01/26/2012, 17:47   #5
 
elite*gold: 21
Join Date: Jul 2005
Posts: 9,193
Received Thanks: 5,380
Going to ignore just for now all the things donn is mentioning (he's correct. Just clearing everything on a form will not just 'make it go away') and focus on the actual error.


In C# (well... basically any object oriented programming language) you can either have static objects (can only have one of them) or you can have instances of objects.

Forum1 is NOT a static object. You could (if you wanted to) create all sorts of copies of Forum1 and display them. The error you are getting is because you are not saying WHICH Forum1 you want to perform the action on...

It's like saying "Car.Unlock()" without saying what car to try to unlock.

The reason you get this error inside your void and not within the event handler (button press) is because that button press was already inside the Forum1 class... Therefor you can operate on it because everything is already local to it.

Think of this like being inside a car and me telling you to unlock the doors. It simply makes sense that I'm talking about the car you're inside... not a car that is across the parking lot.

Now... your method has no reference to the instance of a Forum1 object. As such it cannot do what you're asking of it.


Here's a simple (although not recommended) workaround to give you an idea of how it works...


Inside the forum1 code do something like...

public static Forum1 MainWindow;

Then inside the initialization of the forum say...

MainWindow = this;


Done! You can now just say Forum1.MainWindow.Blablabla

This is NOT recommended, it's just an example of how you could handle it if for some really strange reason you HAD to have access to it outside of itself without first passing any thing between the two separate classes.
pro4never is offline  
Thanks
2 Users
Old 01/26/2012, 18:37   #6
 
elite*gold: 0
Join Date: Dec 2011
Posts: 1,537
Received Thanks: 785
this.Controls.Clear();

or

Form1 f;

Last one needs to be set as a variable somewhere, I assume you have a static field for that, but since anyways it's not doing that by itself, when creating a form, so I assume you call the form within itself and for that reason you can do number one. If it's from another form you have to declare Form1.
I don't have a username is offline  
Thanks
1 User
Old 01/26/2012, 20:36   #7

 
jackpotsvr's Avatar
 
elite*gold: 20
Join Date: Oct 2008
Posts: 328
Received Thanks: 43
Quote:
Originally Posted by pro4never View Post
Going to ignore just for now all the things donn is mentioning (he's correct. Just clearing everything on a form will not just 'make it go away') and focus on the actual error.


In C# (well... basically any object oriented programming language) you can either have static objects (can only have one of them) or you can have instances of objects.

Forum1 is NOT a static object. You could (if you wanted to) create all sorts of copies of Forum1 and display them. The error you are getting is because you are not saying WHICH Forum1 you want to perform the action on...

It's like saying "Car.Unlock()" without saying what car to try to unlock.

The reason you get this error inside your void and not within the event handler (button press) is because that button press was already inside the Forum1 class... Therefor you can operate on it because everything is already local to it.

Think of this like being inside a car and me telling you to unlock the doors. It simply makes sense that I'm talking about the car you're inside... not a car that is across the parking lot.

Now... your method has no reference to the instance of a Forum1 object. As such it cannot do what you're asking of it.


Here's a simple (although not recommended) workaround to give you an idea of how it works...


Inside the forum1 code do something like...

public static Forum1 MainWindow;

Then inside the initialization of the forum say...

MainWindow = this;

Done! You can now just say Forum1.MainWindow.Blablabla

This is NOT recommended, it's just an example of how you could handle it if for some really strange reason you HAD to have access to it outside of itself without first passing any thing between the two separate classes.
Ah I see, well I don't want to clear the things from memory orso, since the thing I will clear is just UserComponent1 so I can get to UserComponent2 (and I want to be able to switch between those, so clearing it from a memory will just make the program running slow I guess). But isn't there anyways to make a void doing the 'Form1.Controls.Clear();' ?
jackpotsvr is offline  
Old 01/26/2012, 22:16   #8
 
elite*gold: 20
Join Date: Aug 2007
Posts: 1,749
Received Thanks: 2,199
Quote:
Originally Posted by jackpotsvr View Post
Ah I see, well I don't want to clear the things from memory orso, since the thing I will clear is just UserComponent1 so I can get to UserComponent2 (and I want to be able to switch between those, so clearing it from a memory will just make the program running slow I guess). But isn't there anyways to make a void doing the 'Form1.Controls.Clear();' ?
Save the instance of the form in a global variable called myForm or something. Then, from your "void", you call myForm.Controls.Clear();
You could assign your global variable (myForm) a value in your Main procedure (or somewhere else, it's all up to you).

Something like this:

(Program.cs)
Code:
    static class Program
    {

        private static Form1 myForm;

        public static Form1 MyForm
        {
            get { return myForm; }
        }

        [STAThread]
        static void Main()
        {

            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            myForm = new Form1();

            Application.Run(myForm);

        }
    }
Then, when you want to refer to your form from inside your own method, you just do

Code:
        public void SwitchUC()
        {
            Program.MyForm.Controls.Clear();
        }
Many will probably argue that this is not the ideal approach, but I doubt you care much about design anyways.
IAmHawtness is offline  
Thanks
2 Users
Old 01/26/2012, 23:00   #9

 
jackpotsvr's Avatar
 
elite*gold: 20
Join Date: Oct 2008
Posts: 328
Received Thanks: 43
Quote:
Originally Posted by IAmHawtness View Post
Save the instance of the form in a global variable called myForm or something. Then, from your "void", you call myForm.Controls.Clear();
You could assign your global variable (myForm) a value in your Main procedure (or somewhere else, it's all up to you).

Something like this:

(Program.cs)
Code:
    static class Program
    {

        private static Form1 myForm;

        public static Form1 MyForm
        {
            get { return myForm; }
        }

        [STAThread]
        static void Main()
        {

            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            myForm = new Form1();

            Application.Run(myForm);

        }
    }
Then, when you want to refer to your form from inside your own method, you just do

Code:
        public void SwitchUC()
        {
            Program.MyForm.Controls.Clear();
        }
Many will probably argue that this is not the ideal approach, but I doubt you care much about design anyways.
Good works great. Anyways, what would be wrong if I wanted to do some designing in here?
jackpotsvr is offline  
Old 01/26/2012, 23:53   #10
 
elite*gold: 20
Join Date: Aug 2007
Posts: 1,749
Received Thanks: 2,199
Quote:
Originally Posted by jackpotsvr View Post
Good works great. Anyways, what would be wrong if I wanted to do some designing in here?
I don't mean design as in visual design. I just meant that normally you'd have the method "SwitchUC" in the UI class itself. It will also make everything a lot easier if you ever introduced multi-threading to your program.
IAmHawtness is offline  
Old 01/27/2012, 15:07   #11

 
jackpotsvr's Avatar
 
elite*gold: 20
Join Date: Oct 2008
Posts: 328
Received Thanks: 43
Quote:
Originally Posted by IAmHawtness View Post
I don't mean design as in visual design. I just meant that normally you'd have the method "SwitchUC" in the UI class itself. It will also make everything a lot easier if you ever introduced multi-threading to your program.
Oh well in this case im not really planning on doing multithreading, although good to know. I didn't need the void later anyways, since the UserComponent I was wishing to call this void from, can either just do that line of code itself.

The reason I want this is because I use one form, that use multiple usercomponents to switch between interfaces.
Now how I fixed it is
ButtonClick --
Program.MyForm.Controls.Clear();
Program.MyForm.Controls.Add(US2) (US2 is an usercomponent I assigned.
jackpotsvr is offline  
Old 01/27/2012, 19:56   #12
 
elite*gold: 21
Join Date: Jul 2005
Posts: 9,193
Received Thanks: 5,380
ummm.... maybe I'm misunderstanding you but you're using a single forum and instead of separating things based on control groups and swapping between them you're simply hiding all controls and adding/showing a different control then swapping back?....

No offense but that's really not the right way to be handling things...

If for some reason you MUST have multiple 'states' for your forum then I'd suggest writing a screenstate type system (mostly used on game programming type environments where you need various gamestats such as menu screen, gameplay, paused etc while within the same program). The idea is that you create an object which represents the current state of the screen and then within the main program (form) you track and can easily swap between them using simple methods rather then the way you're handling it right now.

That being said... if I'm understanding you correctly (unlikely, I tend to make big leaps when I read things ahhaa) then you're prob best off just using some sort of winform control like tab controls.
pro4never is offline  
Old 01/27/2012, 23:19   #13

 
jackpotsvr's Avatar
 
elite*gold: 20
Join Date: Oct 2008
Posts: 328
Received Thanks: 43
Quote:
Originally Posted by pro4never View Post
ummm.... maybe I'm misunderstanding you but you're using a single forum and instead of separating things based on control groups and swapping between them you're simply hiding all controls and adding/showing a different control then swapping back?....

No offense but that's really not the right way to be handling things...

If for some reason you MUST have multiple 'states' for your forum then I'd suggest writing a screenstate type system (mostly used on game programming type environments where you need various gamestats such as menu screen, gameplay, paused etc while within the same program). The idea is that you create an object which represents the current state of the screen and then within the main program (form) you track and can easily swap between them using simple methods rather then the way you're handling it right now.

That being said... if I'm understanding you correctly (unlikely, I tend to make big leaps when I read things ahhaa) then you're prob best off just using some sort of winform control like tab controls.
The way it works is I have a windows form, and multiple usercompononents. (For example UC1 = LoginScreen). On the login screen I have 2 masked textboxes, ID, Pass connected to mysql). After the button is clicked and everything is filled in correctly the form will clear and will add usercomponent2which is a form with anything else inside it. Etcetc.

I don't really know what's another way to accomplish this. I don't want to switch forms, and with UserComponents I still have a designer.

Anyways I must admit im not an too experienced programmer.. Maybe I should take care of some more basics and follow some course I can find here in this section .
jackpotsvr is offline  
Old 01/29/2012, 22:06   #14
 
elite*gold: 0
Join Date: Jan 2012
Posts: 85
Received Thanks: 23


IAmHawtness>>thx so much for ur job =)
Heroka is offline  
Reply


Similar Threads Similar Threads
reference Exception
11/28/2011 - CO2 Private Server - 6 Replies
Well smtimes i face Errors of type Null Reference Exception what does it means ??
[HOT] Reference Sites
11/14/2011 - Facebook - 0 Replies
For FB Cheats: For-The-Cheater-And-Hacker PWNTHIS I will not include secret forums BTW... KIXEYE has SPY.
Looking for a reference
11/08/2011 - Dekaron Private Server - 8 Replies
We are sorry but we just got this letter and we comply with the msg " To Elitepvpers The account Cataracts - Daemon Strother has been reported for multiple crimes by the US state department of illegal Child sexual abuse! law § 2243 - Sexual abuse of a minor or ward: (a) Of a Minor.— Whoever, in the special maritime and territorial jurisdiction of the United States or in a Federal prison, or in any prison, institution, or facility in which persons are held in custody by direction of or...
an object reference is required for the nonstatic field method or property
10/31/2011 - CO2 Private Server - 7 Replies
hello guys , i am getting this error in : public static void WaitForWinner() and here is a pic of the error. http://img528.imageshack.us/img528/5937/unledxzg. jpg and here is my class which have the 2 error's
just a reference
11/26/2008 - Dekaron Exploits, Hacks, Bots, Tools & Macros - 0 Replies
havnt been on for some time i dont know if its any help at all this is an old sheet i used to use to find things quicker with winhex though id post it :) If someone else has posted it im sorry in advance :handsdown: the way i use it is simple just say i wanted to find a Great mana potion all i would do is click the search tab in winhex then find text then enter 3806 then it should be the first item that pops up with a line that looks like 3806,Great Mana...



All times are GMT +1. The time now is 22:56.


Powered by vBulletin®
Copyright ©2000 - 2026, Jelsoft Enterprises Ltd.
SEO by vBSEO ©2011, Crawlability, Inc.
This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Support | Contact Us | FAQ | Advertising | Privacy Policy | Terms of Service | Abuse
Copyright ©2026 elitepvpers All Rights Reserved.