1

I'm very new to C#. Coming from pure C/C++ background. Hence please go easy on me, if my question is basic.

I'm trying to declare an object of struct Abc which is complaining a compilation error as below which means that, the object obj is NOT recognized.

        string mains1 = obj.s1;

error CS0120: An object reference is required for the non-static field, method, or property 'csharp1.Program.obj'

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace csharp1
{
    class Program
    {
        public struct Abc
        {
            public string s1;
        };

        Abc obj = new Abc();;

        static void Main(string[] args)
        {
            System.Console.WriteLine("start main");
            string cwd = Directory.GetCurrentDirectory();
            string fileName = "Myfile.xml";
            string destFile = System.IO.Path.Combine(cwd, fileName);
            string inpFile = "MyfileINP.xml";
            string inpDir = System.IO.Path.Combine(cwd, "SubDir");
            string srcfile = System.IO.Path.Combine(inpDir,inpFile);
            File.Copy(srcfile, destFile, false);
            string mains1 = obj.s1;
        }
    }
}

Not sure what is the error here.

7
  • 4
    This is a general static/non-static mater. You should have to deal with the same stuff in C++ OOP. Commented Mar 29, 2018 at 13:57
  • It's because you've declared Abc obj = new Abc() as an instance member, not a class member. static Abc obj = new Abc(); will fix the issue. Commented Mar 29, 2018 at 13:58
  • Main is a static method, so you either need to explicitly construct an instance of Program, or, more likely, change Abc obj to static Abc obj Commented Mar 29, 2018 at 13:58
  • One thing to keep in mind that while Native C++ Struct and Class are related (so much so the only difference is the defautl accessor), in .NET they are not. Structs belong to the general are of value types. They use different memory allocation stategy (a lot less overhead), they are boxed - not cast - into objects. By default they compare by value, not by reference. So the difference is relevantly. Just do not get stuck on the whole heap/stack thing (blogs.msdn.microsoft.com/ericlippert/2009/04/27/…) Commented Mar 29, 2018 at 13:58
  • { } do not balance and what with the extra ; Commented Mar 29, 2018 at 14:00

3 Answers 3

4

This is complaining because you are trying to access a non static instance "obj" from a static context the Main method so there are multiple ways to resolve it you can make that instance static too like "static Abc obj=new Abc()" or you can shift that declaration inside Main method or you can create an instance of Program class and then use that instance.

METHOD 1:

            static  Abc obj = new Abc();

            static void Main(string[] args)
            {
                System.Console.WriteLine("start main");
               //
                string mains1 = obj.s1;
            }

METHOD 2:

 static void Main(string[] args)
        {
            Abc obj = new Abc();
            System.Console.WriteLine("start main");
           //
            string mains1 = obj.s1;
        }

METHOD 3:

Abc obj = new Abc();

        static void Main(string[] args)
        {
            System.Console.WriteLine("start main");
            //
            string mains1 = new Program().obj.s1;
        }

Its always not a good practice to make "static" everywhere until there is no other option you have, making static allow this object to be shared across all the instances of this class so its not good thing until we really need that.

Sign up to request clarification or add additional context in comments.

4 Comments

Please explain what the OP's issue was, what you have changed and why.
Please edit the answer rather than as a comment. Once done, please delete the comment :)
@Irshad: Since main() is static in C#, in that case no member function of a class (having main) can access any data member of that class which is not static. Because, main() which is static has to call only another static function and again static member functions can't access Non_static data members . So Basically all data members remain static in C#. Am i missing something ?
Yes sometimes members of a class having Main() are static just to make them accessible in the method so they can be called but we have other choice too to get accessibility of those members which are non static as usually they are in programs maintaining OOP concepts, we just need to create an instance of the container class as you see in my code example
2

You need to create an instance of the containing class Program before accessing it's member(s) like

string mains1 = new Program().obj.s1;

6 Comments

Would this be advised over just making obj static?
@Frontear, why not?
Just curious, I'm a student learning C#, not the best programmer around :).
@Frontear, that's what I said ... I don't see any issue. Just cause the member is accessed inside a static method doesn't mean it has to be declared static
@Frontear Its always not a good practice to make "static" everywhere until there is no other option you have, making static allow this object to be shared across all the instances of this class so its not good thing until we really need that
|
2

You are trying to access obj (a non-static method) from within Main (a static method). Because of this, your are getting CS0120. I advise changing Abc obj = new Abc() to static ABC obj = new Abc().

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.