Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用foreach循环从结构列表访问字段_C#_List_Loops - Fatal编程技术网

C# 使用foreach循环从结构列表访问字段

C# 使用foreach循环从结构列表访问字段,c#,list,loops,C#,List,Loops,对于任何在这个问题上结结巴巴的人。不幸的是,在我有机会用错误信息修改它之前,它被过于热心的评论员否决了,现在已经包括在内了。问题依然存在,答案也依然存在 我有一个这样的结构 public struct note{ public note (double SampleTime, string Label) { sampleTime = (float)SampleTime; label = Label; } float sampleT

对于任何在这个问题上结结巴巴的人。不幸的是,在我有机会用错误信息修改它之前,它被过于热心的评论员否决了,现在已经包括在内了。问题依然存在,答案也依然存在

我有一个这样的结构

public struct note{

    public note (double SampleTime, string Label)
    {
        sampleTime = (float)SampleTime;
        label = Label;
    }
    float sampleTime;
    string label;
}
我已经声明了一个列表,它将由新的note结构组成

public List<note> notesList;
然而,这是行不通的

`RthymGameUtilityReadFile.note.sampleTime由于其保护级别而无法访问

谢谢


Jim

因为您的结构中没有任何公共字段

该字段不可访问,因此无法使用

把它公之于众,让它成为你的财产

public struct note
{
    public note (double sampleTime, string label)
    {
        SampleTime = (float)sampleTime;
        Label = label;
    }

    public float SampleTime { get; private set; }
    public string Label { get; private set; }
}

C#中的默认访问修饰符是
private
。您的
struct
只有私有字段,因此您不能在它之外访问它们。添加公共属性以获取值:

public struct Note
{
    float sampleTime;
    string label;

    public Note(double SampleTime, string Label)
    {
        sampleTime = (float)SampleTime;
        label = Label;
    }

    public float SampleTime { get { return sampleTime; } }
    public string Label { get { return label; } }
}

“不起作用”对我们没有帮助-在什么方面它不起作用?你是对的。我应该在控制台中读取错误。感谢您查看“RhythmatGameUtilityReadFile.note.sampleTime”由于其保护级别而无法访问。我的问题也没有帮助。添加这个问题很容易,而且这个问题可能对其他人也有帮助。与其抱怨反对票,不如从你收到的评论中吸取教训,下次再发布一个更好的问题。虽然有些人相对容易否决投票(主要是问题),但满足基本SO期望的帖子很少被否决。如果您仍然不明白为什么会不断获得否决票,您应该(更深入地)查看帮助页面和/或阅读其他问题,以了解您做错了什么。还请记住,这不仅仅是关于你,也是关于未来的读者:自己做一些努力,确保这之前没有被问过,等等。我的帖子对其他人很有用,包含了错误信息,并且很好地展示了标签、错误信息、清晰的主题和示例代码。我从来没有让我的帖子被否决过,这是第一次,也是错误的。还记得varocarbas——如果你没有提供任何有用的问题,而是评论堆栈溢出礼仪,那么我建议你一定要充分阅读这篇文章。如果你有编辑的建议,请说。如果你这样做,我相信你会喜欢堆栈溢出!完全正确,谢谢!错误为“ThathmGameUtilityReadFile.note.sampleTime”由于其保护级别而无法访问。我应该检查一下。
public struct note
{
    public note (double sampleTime, string label)
    {
        SampleTime = (float)sampleTime;
        Label = label;
    }

    public float SampleTime { get; private set; }
    public string Label { get; private set; }
}
public struct Note
{
    float sampleTime;
    string label;

    public Note(double SampleTime, string Label)
    {
        sampleTime = (float)SampleTime;
        label = Label;
    }

    public float SampleTime { get { return sampleTime; } }
    public string Label { get { return label; } }
}