Apologies if this has been addressed before as I'm new to coding and don't recognize half the code going on.
I'm trying to use a field/variable in the contents of the File.WriteAllText
method, but I get an error
CS1020 - An object reference is required for the non static field, method, or property "Program.writePath"
I don't exactly know what this means.
I know that writing the string directly into the method fixes the error, and that there's an automatic fixing tool in my IDE that wants to put argument names "path" and "contents" before each thing (though that doesn't fix it). Why is this the case? How do I use my fields in the method instead of typing them in directly?
Here is the code:
using System;
using System.IO;
namespace w3consoleapp30_Cs_files
{
class Program
{
public string writePath = "filename.txt";
public string writeText = "Hello, World!";
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
File.WriteAllText(writePath, writeText);
}
}
}
public string writePath
topublic const string writePath
. Ditto forwriteText
. Also, consider using named-parameters in the call toWriteAllText
as both parameters are typed asString
. – DaiProgramData
) – Jimi