I created Settings class which I use to edit my application by .ini file. My Settings.ini file looks like this:
[ACCOUNT]
login=xyz
password=xyz
locations=1,2,5,8
Now I am getting theese values like this:
class Settings {
public static IniFile Config = new IniFile(Directory.GetCurrentDirectory() + @"\Settings.ini");
public static string Login { get { return Config.Read("login", "ACCOUNT"); } set { Config.Write("login", "ACCOUNT"); } }
public static string Password { get { return Config.Read("password", "ACCOUNT"); } set { Config.Write("password", "ACCOUNT"); } }
public static int[] Locations { get { return Array.ConvertAll(Config.Read("locations", "ACCOUNT").Split(','), s => int.Parse(s)); } set { Config.Write("locations", "ACCOUNT"); } }
}
The problem is, when my Settings.ini file has empty locations:
locations=
My variable Settings.Locations return null instead of empty array. I've tried doing something like this:
public static int[] Locations
{
get { return new int[] {Array.ConvertAll(Config.Read("locations", "ACCOUNT").Split(','), s => int.Parse(s))}; }
set { Config.Write("locations", "ACCOUNT"); }
}
But that is just not working. I cant convert int[] to int. Do you have any ideas how can I return empty array?
Config.Read("locations", "ACCOUNT")returns null if there are no locations? Because yourLocationsproperty never returns null becauseArray.ConvertAllnever returns null. But ifConfig.Readreturns nullstring.Splitthrows an exception.Format exceptionArray.ConvertAllyou should use a loop andint.TryParseto try-parse every token. Then you can handle invalid input