本文介绍了更改代码,以便将对象添加到ArrayList的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

•现在必须将每个CSub对象添加到ArrayList(作为方法参数提供),而不是添加到列表框lstSub
•加载所有记录后,该方法必须返回0

• Each CSub object must now be added to the ArrayList (supplied as a method parameter), and not to listbox lstSub
• After all the records were loaded, the method must return 0

IFormatter serializer;
FileStream subFile;
CSub newSub;
try
{
    if (File.Exists("Submarines.bin"))
    {
        lstSub.Items.Clear();
        serializer = new BinaryFormatter();
        subFile = new FileStream("Submarines.bin", FileMode.Open, FileAccess.Read);

        long fileLength = subFile.Length;
        while (subFile.Position < fileLength)
        {
            newSub = new CSub();
            newSub = serializer.Deserialize(subFile) as CSub;
            lstSub.Items.Add(newSub);
        }
        subFile.Close();
        UC.MB("Submarines Loaded", lstSub.Items.Count + " submarine records were successfully loaded from the file Submarines.bin");
    }
    else
    {
        UC.MB("File not found", "Could not find the file Submarines.bin");
    }
}
catch (Exception ex)
{
    UC.MB("mnuiLoadSub_Click", ex.Message);
}
finally
{
    serializer = null;
    subFile = null;
    newSub = null;
}

推荐答案


这篇关于更改代码,以便将对象添加到ArrayList的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-28 04:58