简体   繁体   English

无法从“无效”转换为“ System.Collections.Generic.List” <string> C#

[英]cannot convert from 'void' to 'System.Collections.Generic.List<string> C#

I'm trying to add a new List for each loop in the code below to a dictionary, but I'm currently struggling with the following error: 我正在尝试将以下代码中的每个循环的新列表添加到字典中,但是我目前正遇到以下错误:

cannot convert from 'void' to 'System.Collections.Generic.List 无法从“无效”转换为“ System.Collections.Generic.List”

public class WeaponGen{
Dictionary<string, List<string>> weapons = new Dictionary <string, List<string>>(); 

public void WeaponGeneration(int GenerationCount)
{
    Weapon weapon = new Weapon(); //this class contains basic calculation methods
    weapons.Clear();

    for (int i = 0; i < GenerationCount; i++)
    {
        string listname = "weapon" + i;
        string id = Random.Range(1, 41).ToString();

        List<string>[] _lists = new List<string>[GenerationCount];
        _lists[i] = new List<string>();

        weapons.Add(listname, _lists[i].Add(id)); //this line throws an error
        weapons.Add(listname, _lists[i].Add(weapon.GetName(id))); //this line throws an error
        weapons.Add(listname, _lists[i].Add(weapon.GetRarity(id))); //this line throws an error
        weapons.Add(listname, _lists[i].Add(weapon.GetType(id))); //this line throws an error
        weapons.Add(listname, _lists[i].Add(weapon.GetDmg(id).ToString())); //this line throws an error
        weapons.Add(listname, _lists[i].Add(weapon.GetSpeed(id).ToString())); //this line throws an error
        weapons.Add(listname, _lists[i].Add(weapon.GetCost(id).ToString())); //this line throws an error
    }
}}

Since my coding skills are definitly lacking in some aspects, I figured someone with a better grasp on the language could help me out. 由于某些方面确实缺乏我的编码技能,因此我认为对语言有更好了解的人可以为我提供帮助。 Every help is greatly appreciated! 非常感谢您的帮助!

Welcome to SO! 欢迎来到SO!

List add method does not return any value: https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.list-1.add?view=netframework-4.7.2 . 列表add方法不返回任何值: https : //docs.microsoft.com/zh-cn/dotnet/api/system.collections.generic.list-1.add?view=netframework-4.7.2

So, if I understood your idea correctly, you need to fill the list with needed values and add it itself to the dictionary, like this: 因此,如果我正确理解了您的想法,则需要用所需的值填充列表,并将其本身添加到字典中,如下所示:

for (int i = 0; i < GenerationCount; i++)
{
    string listname = "weapon" + i;
    string id = Random.Range(1, 41).ToString();

    List<string>[] _lists = new List<string>[GenerationCount];
    _lists[i] = new List<string>();

    _lists[i].Add(id); 
    _lists[i].Add(weapon.GetName(id)); 
    _lists[i].Add(weapon.GetRarity(id)); 
    _lists[i].Add(weapon.GetType(id)); 
    _lists[i].Add(weapon.GetDmg(id).ToString());
    _lists[i].Add(weapon.GetSpeed(id).ToString()); 
    _lists[i].Add(weapon.GetCost(id).ToString());
    weapons.Add(listname, _lists[i]);
}

PS What is it Random.Range ? PS什么是Random.Range Is it some extention method? 是某种扩展方法吗? In any case, it seems to be dangerous to generate id's based on random value in such small interval. 无论如何,在如此小的间隔内基于随机值生成id似乎很危险。 Why not simple use i.ToString() ? 为什么不简单使用i.ToString()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C#:无法隐式转换类型“System.Collections.Generic.List”<char> &#39; 到 &#39;System.Collections.Generic.List<string> &#39; - C#: Cannot implicitly convert type 'System.Collections.Generic.List<char>' to 'System.Collections.Generic.List<string>' 无法从“字符串”转换为“system.collections.generic.list” - Cannot convert from 'string' to 'system.collections.generic.list 无法将类型“字符串”隐式转换为“System.Collections.Generic.List”<string> &#39; c# 类 getter - Cannot implicitly convert type 'string' to 'System.Collections.Generic.List<string>' c# class getter C# SelectMany 错误“无法隐式转换类型‘System.Collections.Generic.List<char> ' 到 'System.Collections.Generic.List<list> ’”</list></char> - C# SelectMany error " Cannot Implicitly convert type 'System.Collections.Generic.List<char>' to 'System.Collections.Generic.List<List>' " Cannot convert type 'System.Collections.Generic.List&lt; &gt;' to 'System.Collections.Generic.IList&lt; &gt;' c# MongoDB - Cannot convert type 'System.Collections.Generic.List< >' to 'System.Collections.Generic.IList< >' c# MongoDB 无法将类型&#39;void&#39;隐式转换为&#39;System.Collections.Generic.List <string> “ - Cannot implicitly convert type 'void' to 'System.Collections.Generic.List<string>' 参数 1:无法从 'System.Collections.Generic.List' 转换为 'System.Collections.Generic.List' - Argument 1: cannot convert from 'System.Collections.Generic.List' to 'System.Collections.Generic.List' C#无法隐式转换类型&#39;System.Collections.Generic.List <string> &#39;到&#39;System.Windows.Documents.List&#39; - C# Cannot implicitly convert type 'System.Collections.Generic.List<string>' to 'System.Windows.Documents.List' 参数“#2”不能转换“System.Collections.Generic.List”<string> - Argument `#2' cannot convert `System.Collections.Generic.List<string> 无法将类型&#39;System.Collections.Generic.List&#39;隐式转换为&#39;string&#39; - Cannot implicitly convert type 'System.Collections.Generic.List' to 'string'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM