Im trying to set an Array or a List in the config and then loop through it but I failed :c
Idk if Im just too stpd...
What I tried:
Code://---------------------------------------------------------------------------------// protected override void LoadDefaultConfig() { Config.Clear(); Config["Test"] = new string[] { "Hello", "World!" }; SaveConfig(); }//---------------------------------------------------------------------------------// protected override void LoadDefaultConfig() { Config.Clear(); Config["Test"] = { "Hello", "World!" }; SaveConfig(); }//---------------------------------------------------------------------------------// protected override void LoadDefaultConfig() { Config.Clear(); Config["Test"] = new List<string> { "Hello", "World!" }; SaveConfig(); }//---------------------------------------------------------------------------------// foreach(string current in Config["Test"]) { Puts(current); }
Solved Array or List in config (C#)
Discussion in 'Rust Development' started by LaserHydra, Jun 18, 2015.
-
Code:
protected override void LoadDefaultConfig() { var listIsList = new List<object>() { "item1", "item2" }; Config["ListIsList"] = listIsList; }
-
EDIT: Ouhh you used a list of objects... I tried a list of strings
[DOUBLEPOST=1434664679][/DOUBLEPOST]@Hatemail Looping through still does not work :c
Code:foreach statement cannot operate on variables of type `object' because it does not contain a definition for `GetEnumerator' or is inaccessible
Last edited by a moderator: Jun 18, 2015 -
Use Dictionary, good object
Code:public Dictionary<string, Dictionary<int, Dictionary<string, string>>> WorldTables = new Dictionary<string, Dictionary<int, Dictionary<string, string>>>();
-
Yeah and what am I suppose to do with that? You don't post your code so I don't know the context of the error other than what its says.
Example loop:
Code:var listIsList = new List<object>() { "item1", "item2" }; foreach (var test in listIsList) { Console.WriteLine(test); }
-
Code:5:48 PM [Error] Test plugin failed to compile! 5:48 PM [Error] Test.cs(36,39): error CS1579: foreach statement cannot operate on variables of type `object' because it does not contain a definition for `GetEnumerator' or is inaccessible
Code:protected override void LoadDefaultConfig() { Config.Clear(); var WordList = new List<object>() { "Hello", "World!" }; Config["words"] = WordList; SaveConfig(); } [ChatCommand("test")] void cmdTest(BasePlayer player, string cmd, string[] args) { foreach(var word in Config["words"]) { Puts(word); } }
Last edited by a moderator: Jun 19, 2015 -
Yeah and you expected that to work why? You never converted the object to a List.
The easiest way to do the correct conversion would be.
var listOfConfigPath = Config.Get<List<string>>("ConfigPath"); -
Code:List<string> WordList = Config["words"] as List<string>;
Last edited by a moderator: Jun 19, 2015