So im making a config system, i know the code is mesy as ****, i try to add a item to a list but it fails cause the list seems to be: 'null'
i somehow need to refrence the: configElements list but i couldn't find out how, i dug through some docs and old stackoverflow questions, if this question has been answerd before im very sorry but i couldn't find the question, also sorry for my questionable english, its not my main language(?)
Here is the code i got so far, i would thank you very much if you could explain the issue.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace ConfigTest
{
class Config
{
private string configFile;
private List<string> configElements;
public Config(string configPath = "unset")
{
this.configFile = configPath;
}
//Checks if the config file allready exists
public string makeConfig()
{
if (File.Exists(configFile))
{
return "Config file allready exists: " + configFile;
}
File.Create(configFile);
return "Made Config file!";
}
public string getString(string element)
{
string[] configContent = getConfigContentL();
foreach(string line in configContent)
{
if (line.Contains(element))
{
return line.Replace(element + 1, "");
}
return "Couldn't find the element: " + element;
}
return "Couldn't open config file!";
}
private void appendElementToList(string element)
{
if (!this.configElements.Contains(element))
{
this.configElements.Add(element);
}
}
public void setString(string element, string content)
{
string[] configContent = getConfigContentL();
appendElementToList(element);
if (configElements.Contains(element))
{
foreach(string line in configContent)
{
if (line.Contains(element))
{
string editElement = getString(element);
line.Replace(editElement, content);
}
}
}
else
{
File.AppendAllText(configFile , element + " " + content);
}
}
public string getConfig()
{
return configFile;
}
public string getConfigContentF()
{
return File.ReadAllText(configFile);
}
public string[] getConfigContentL()
{
return File.ReadAllLines(configFile);
}
public List<string> getElements()
{
return configElements;
}
}
}