Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5838387
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T11:27:45+00:00 2026-05-22T11:27:45+00:00

hi i have this lines of code that i cant make it work the

  • 0

hi i have this lines of code that i cant make it work
the goal is simple setting the form1 to visible = false

public static void DoActions(string Cmd){

if(Cmd == true)
{

MainForm.Visible = false;
}
}

but i keep on having this error

An object reference is required for
the non-static field, method, or
property

usually i set the called methond to static.. so the error will go away
but on this case how do i do it?

thanks for any help guys

‘System.Windows.Forms.Control.Invoke(System.Delegate)’

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T11:27:45+00:00Added an answer on May 22, 2026 at 11:27 am

    This is happening because DoActions is a static method rather than an instance method, however MainForm is an instance field / property. The distinction is that instance methods operate on an instance of the class on which they are defined, wheras static methods do not.

    This means that wheras instance methods are able to access properties, fields and methods of their containing class through the this keyword, for example:

    // Instance field
    Form1 MainForm;
    
    void InstanceMethod()
    {
        Form1 frm = this.MainForm;
    }
    

    You cannot do the same thing from inside a static method (think about it, what instance would it operate on?). Note that C# will implicitly assume the use of the this keyword in places where it makes sense (so the above example could have been written as Form1 frm = MainForm).

    See C# Static Methods for an alternative explanation of static vs instance methods (this is an important concept in object oriented programming that you should take the time to understand properly).

    In your example you probably want to change DoActions to an instance method (by removing the static declaration):

    public void DoActions(string Cmd)
    {
        if(Cmd == true)
        {
            this.MainForm.Visible = false;
        }
    }
    

    This will allow it to access the instance MainForm field / property, however this may cause problems elsewhere in your code in places where you attempt to call DoActions from another static method without supplying an object instance.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this legacy code base (Compaq PERL) , about 1500 lines of code,
So I have this code in python that writes some values to a Dictionary
I have this line of code for page load: if ($(input).is(':checked')) { and it
Say I have this line of code in the view. <?php echo CHtml::activeTextField($model,'start_time'); ?>
Sorry, I'm new to flash I have this line of code: BaseEntry( _entryList[i] ).topTeamName
I have this code in my controller and want to test this code line
I have a this line of code: var predicate = Expression.Lambda<Func<TEntityType, bool>>(body, param); where
I have a greasemonkey user script with this single line of code... window.close(); but
I have 1 compiler error. It is from this line in my code: cout
Hey I have this code right here: http://pastie.org/534470 And on line 109 I get

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.