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#:列表中填充的是相同的类实例,而不是新的类实例_C#_List_Class_Instance - Fatal编程技术网

C#:列表中填充的是相同的类实例,而不是新的类实例

C#:列表中填充的是相同的类实例,而不是新的类实例,c#,list,class,instance,C#,List,Class,Instance,我试图从内存流中读取一个文件,并将其内容组织在一个实例列表中。文件中的某些行具有具有相应类的特定类型,并且同一类型的多行可以出现在文件中 我当前的代码如下所示: public void initializeStructure( System.IO.MemoryStream RawFile ) { int positioninFile = 0; while (!reachedEndOfFile)

我试图从内存流中读取一个文件,并将其内容组织在一个实例列表中。文件中的某些行具有具有相应类的特定类型,并且同一类型的多行可以出现在文件中

我当前的代码如下所示:

       public void initializeStructure( System.IO.MemoryStream RawFile )
       { 
            int positioninFile = 0;
            while (!reachedEndOfFile)
            {
                string recordType = GetRecordType(RawFile, positioninFile);
                int ListPosition = MessageStructure.Count;

                switch (recordType)
                {
                    case "01":
                        Class_01 _01 = new Class_01(Params);
                        MessageStructure.Add(_01);
                        break;
                    case "02":
                        Class_02 _02 = new Class_02(Params);
                        MessageStructure.Add(_02);
                        break;
                    case "04":
                        Class_04 _04 = new Class_04(Params);
                        MessageStructure.Add(_04);
                        break;
                    default:
                        reachedEndOfFile = true;
                        break;
                }

                string row = GetRowFromMemoryStream(RawFile, 572, positioninFile);
                MessageStructure[ListPosition].WriteRule(row);

                ListPosition++;
                positioninFile += 572;
            }
       }
消息结构定义:

public List<A_Record> MessageStructure = new List<A_Record>();
public List MessageStructure=new List();
这些类的外观示例:

    public class Class_01 : A_Record
    {
        public Class_01( Dictionary<string, A_AbstractClass> Params ) : base(Params)
        {
            RecordType = "01";
            RecordTitle = "Name of record";
        }

    }
public class\u 01:A\u记录
{
公共类_01(字典参数):基(参数)
{
RecordType=“01”;
RecordTitle=“记录名称”;
}
}
记录摘要

    public abstract class A_Record
    {
       public virtual string RecordType { get; set; }
       public virtual string RecordTitle { get; set; }

        public int RecordLength { get; set; }

        public void WriteRule( string Vektis_Rule )
        {
            int writePosition = 0;

            foreach ( KeyValuePair<string, A_Element> Element in Elementen )
            {
                string ElementValue = Vektis_Rule.Substring( writePosition, Element.Value.Length );
                Element.Value.Write( ElementValue );

                writePosition += Element.Value.Length;
            }
        }

        public A_Record(Dictionary<string, A_Gegevenselement> Volgnummers)
        {
            Elementen = Volgnummers;
            RecordLength = 0;

            foreach (KeyValuePair<string, A_Gegevenselement> Element in Elementen)
            {
                var currentElement = Elementen[Element.Key];
                currentElement.StartingPosition = RecordLength;
                RecordLength += currentElement.Length;
            }
        }

    }
公共抽象类A\u记录
{
公共虚拟字符串记录类型{get;set;}
公共虚拟字符串RecordTitle{get;set;}
公共int记录长度{get;set;}
public void WriteRule(字符串Vektis_规则)
{
int writePosition=0;
foreach(Elementen中的KeyValuePair元素)
{
string ElementValue=Vektis_Rule.Substring(writePosition,Element.Value.Length);
Element.Value.Write(ElementValue);
writePosition+=Element.Value.Length;
}
}
公共A_记录(字典卷)
{
Elementen=Volgnummers;
记录长度=0;
foreach(Elementen中的KeyValuePair元素)
{
var currentElement=Elementen[Element.Key];
currentElement.StartingPosition=记录长度;
RecordLength+=currentElement.Length;
}
}
}
所以基本上,对于出现的每一个'04'行,我想向列表中添加一个新的Class_04实例,然后将该行的内容写入该实例

遗憾的是,这并不像我希望的那样有效。如果我将类_04的多个实例添加到列表中,它们都会添加最后一个类_04行的值。这可能意味着列表中的所有类_04条目都是同一个实例,我将不断覆盖它,而不是添加新实例。但我不明白这是怎么回事,每次在While循环中我都在创建一个新实例

有人能帮我吗?正如你所知道的,直到最近我还只使用PHP、Python和Javascript。所以我可能只是在这里有一个d'oh时刻,错过了一些非常明显的东西


提前感谢您的时间和努力

ListPosition
,您将其设置为在循环开始时计数,因此您的增量将被覆盖或是多余的。 如果没有
MessageStructure
的代码,则无法判断,但它看起来不正确

我会完全摆脱
ListPosition
,使用
[MessageStructure.Count]-1
或者更好的
MessageStructure.Last()
而不是索引


我怀疑文件末尾的代码也会出错,因为开关后的代码将执行,并且应该包装在
if(reachedEndOfFile)

中,我无法从这里的代码中分辨出来,但我怀疑您没有为每个记录对象的参数实例化一个新的字典。在
A_Record
类中,有这样一行:
Elementen=Volgnummers存储对传入词典的引用。如果将同一个Dictionary实例与另一个A_Record实例重复使用,它们将具有相同的内容。

hey and welcome,是的,您每次创建一个新实例是正确的,以避免在循环之前声明实例,如
Class_01\u 01=null
然后检查
\u 01==null
,如果是,则通过调用new启动它,如果不是,则仅向列表添加
\u 01
,向列表中添加不同类型的MessageStructure的定义是什么。是的,您正在创建并向列表中添加新的类实例。问题一定在于传递给类的参数或类的定义。你能给我们看看吗?你可以只写
MessageStructure.Add(newclass_01(Params)),因为您从未在此处使用该类引用。另外:您正在覆盖
ListPosition++带有
intlistposition=MessageStructure.Count
in every while循环-冗余,但不是该行为的来源。MessageStructure的定义是
public List MessageStructure=new List()。添加到列表中的所有类都是抽象A_记录的扩展。说得好,我会把这个添加到问题中