更改代码,以便将对象添加到ArrayList [英] Change the code so that objects be added to ArrayList

查看:81
本文介绍了更改代码,以便将对象添加到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;
}

推荐答案

:-D请明确提及您的问题,为什么它应该返回0,代码是什么?请再次引用该问题,并清楚地说明问题.
Prasad
:-D Please mention your question clearly as why should it return 0 and for what the code is? please quote the question again and clearly explain the problem.
Prasad


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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆