Results 1 to 6 of 6

Thread: Method problem.

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2005
    Posts
    537

    Method problem.

    I have no idea what's going on, when I try to run ANY method in a console application, I get some weird error saying "An object reference is required for the nonstatic field, method, or property" what does that mean? If I run this method in an application with a form, it works fine.

    Thanks,
    Sir Loin

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Method problem.

    It means that you are calling a method without creating an instance of the class. It may be that you are using 'this' to refer to the current instance. In a WinForms app that's fine as your code is likely to be in a form. In a console app there are no forms so you cannot use 'this' unless you do so inside a class definition.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2005
    Posts
    537

    Re: Method problem.

    Oh, thanks. I'll have to read more tutorials. I might just give up on C# and go back to C++, C++ seems easier. lol

    Thanks,
    Sir Loin

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Method problem.

    Quote Originally Posted by Sir Loin
    Oh, thanks. I'll have to read more tutorials. I might just give up on C# and go back to C++, C++ seems easier. lol

    Thanks,
    Sir Loin
    I assure you that it is not. All else being equal, C# is definitely easier to come to grips with than C++. Of course, if you're already relatively fluent in C++ then it will of course be easier to use than an unfamiliar language. C# is like the good parts of VB and C++ combined.

    As for the issue that you're having at the moment, that's just straight OOP. You have to have an instance to refer to an instance member. That would be the same in C++.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5
    Fanatic Member BillBoeBaggins's Avatar
    Join Date
    Jan 2003
    Location
    in your database, dropping your tables.
    Posts
    628

    Re: Method problem.

    I realize you dont want to post all of your code, but a piece of it plus your namespace USING list, would help a bit.

    And somehow I cant see someone mastering C/C++ failing at C#.

  6. #6
    KrisSiegel.com Kasracer's Avatar
    Join Date
    Jul 2003
    Location
    USA, Maryland
    Posts
    4,985

    Re: Method problem.

    Quote Originally Posted by Sir Loin
    Oh, thanks. I'll have to read more tutorials. I might just give up on C# and go back to C++, C++ seems easier. lol
    While many will disagree, I do find C++'s console capabilities nicer and possibly easier to use than C#'s.

    Saying that, your problem is an issue with using OOP. Everything in C# is within a Class. Main() is a static method so if you don't declare your methods static, you'll need to create an instance of that class before referencing the methods.
    KrisSiegel.com - My Personal Website with my blog and portfolio
    Don't Forget to Rate Posts!

    Free Icons: FamFamFam, VBCorner, VBAccelerator
    Useful Links: System.Security.SecureString Managed DPAPI Overview Part 1 Managed DPAPI Overview Part 2 MSDN, MSDN2, Comparing the Timer Classes

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width