codeLover
codeLover

Reputation: 3810

Declaring an object of C# struct

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.

Upvotes: 1

Views: 236

Answers (3)

Irshad Ahmed Akhonzada
Irshad Ahmed Akhonzada

Reputation: 1418

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.

Upvotes: 4

Frontear
Frontear

Reputation: 1261

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().

Upvotes: 2

Rahul
Rahul

Reputation: 77866

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

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

Upvotes: 2

Related Questions