public void saveSettings()
{
refreshSettings();
userSettings.sword = swordEquiped;
userSettings.staff = staffEquiped;
userSettings.bow = bowEquiped;
userSettings.overalldamage = overallDamage;
userSettings.silver = silver;
userSettings.gold = gold;
userSettings.exp = exp;
userSettings.level = level;
userSettings.physical = physicalDamage;
userSettings.magic = magicDamage;
userSettings.ranged = rangedDamage;
userSettings.inventory = string.Join(", ", itemsBought.ToArray());
userSettings.Save();
}
public void loadSettings()
{
exp = userSettings.exp;
level = userSettings.level;
swordEquiped = userSettings.sword;
staffEquiped = userSettings.staff;
bowEquiped = userSettings.bow;
overallDamage = userSettings.overalldamage;
silver = userSettings.silver;
gold = userSettings.gold;
physicalDamage = userSettings.physical;
magicDamage = userSettings.magic;
rangedDamage = userSettings.ranged;
nameChosen = userSettings.username;
itemsBought = userSettings.inventory;
}
我有一个名为List<string>
的{{1}},它有3个值,只是项目名称itemsBought
。我想知道如何将列表添加到字符串中,因为这有效:
"Short Sword", "Shortbow", "Staff of Energy"
但是当我尝试加载它时:
userSettings.inventory = string.Join(", ", itemsBought.ToArray());
它无法转换它,给我一个错误itemsBought = userSettings.inventory;
。
答案 0 :(得分:3)
首先,将您的连接字符串更改为","
(无空格),然后尝试:
itemsBought = userSettings.inventory.Split(',').ToList();
或者也许这个(也许你有一些从InventoryItem
类到类型string
的隐式转换,所以需要重新创建那些):
itemsBought = userSettings.inventory
.Split(',')
.Select(s => new InventoryItem(s))
.ToList();
答案 1 :(得分:0)
您需要使用string.Split()
将其转换回数组https://msdn.microsoft.com/en-us/library/b873y76a(v=vs.110).aspx
using System;
public class SplitTest {
public static void Main() {
string words = "This is a list of words, with: a bit of punctuation" +
"\tand a tab character.";
string [] split = words.Split(new Char [] {' ', ',', '.', ':', '\t' });
foreach (string s in split) {
if (s.Trim() != "")
Console.WriteLine(s);
}
}
}
// The example displays the following output to the console:
// This
// is
// a
// list
// of
// words
// with
// a
// bit
// of
// punctuation
// and
// a
// tab
// character