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.
Abc obj = new Abc()as an instance member, not a class member.static Abc obj = new Abc();will fix the issue.Mainis astaticmethod, so you either need to explicitly construct an instance ofProgram, or, more likely, changeAbc objtostatic Abc obj