如何访问数组列表中对象的功能

时间:2019-05-20 01:51:36

标签: c# winforms

我有一个用值创建的名为“ Account”的类(例如“ new Account(amt)”)。但是如何从Account类的对象中检索该金额。我以为我可以使用“ listOfAccounts [0] .balance.ToString();”但是当我尝试使用listOfAccounts [0]时,这些功能似乎不可见。

    private ArrayList listOfAccounts = new ArrayList();
    double amt = (double.Parse(txtAmount.Text));
    listOfAccounts.Add(new Account(amt));
    lblShowBalance.Text = listOfAccounts[0].balance.ToString();

我的课是这样的:

class Account
{
    private double balance;

    public double Balance { get; set; }

    public Account() { }

    public Account (double balance)
    {
        this.balance = balance;
    }

3 个答案:

答案 0 :(得分:2)

可以投放该项目:

 var acct = (Account)listOfAccounts[0];
 acct.balance.ToString();

但是不要那样做!

自.Net 2.0大约15年前引入泛型以来,.Net中的

ArrayList已过时。没有充分的理由永远使用新代码。而是使用List<T>,它是相同的东西,但是要强类型输入。

虽然我在这里:使用double类型赚钱也不是一件容易的事。处理金钱时,应始终使用decimal

像这样将它们放在一起:

public class Account
{
    public decimal Balance {get; set;}

    public Account() { }
    public Account(decimal balance)
    {
        this.Balance = balance;
    }
}

class Program
{
    static void Main(string[] args)
    {
        var listOfAccounts = new List<Account>();
        listOfAccounts.Add(new Account(133.1m));

        Console.WriteLine(listOfAccounts[0].Balance);
        Console.ReadKey(true);
    }
}

并带有一个有趣的集合初始化器:

class Program
{
    static void Main(string[] args)
    {
        var listOfAccounts = new List<Account> {new Account(133.1m)};
        Console.WriteLine(listOfAccounts[0].Balance);
        Console.ReadKey(true);
    }
}

答案 1 :(得分:1)

您可以将元素强制转换为Account,然后可以访问 public 成员:

private ArrayList listOfAccounts = new ArrayList();
double amt = (double.Parse(txtAmount.Text));
listOfAccounts.Add(new Account(amt));
lblShowBalance.Text = ((Account)listOfAccounts[0]).Balance.ToString();

您的班级应该看起来像这样以获取余额值:

class Account
{
  private double balance;

  public double Balance { get { return balance; } }

  public Account() { }

  public Account (double balance)
  {
      this.balance = balance;
  }
}

要将所有元素强制转换为强类型,请查看here

答案 2 :(得分:1)

问题是ArrayList存储对象。您想将其称为Account而不是Object(就像其他对象继承的基础对象类型一样)。 代码存在一些问题,但是要回答您的问题,您可以将该对象转换回Account类型:

class Program
{
    static void Main(string[] args)
    {
        ArrayList listOfAccounts = new ArrayList();
        double amt = double.Parse("133.1");
        listOfAccounts.Add(new Account(amt));
        Console.WriteLine(((Account)listOfAccounts[0]).balance.ToString());
        Console.ReadLine();
    }
}

public class Account
{
    public double balance;
    public Account() { }
    public Account(double balance)
    {
        this.balance = balance;
    }
}

但是理想情况下,您根本不应该使用ArrayListhttps://docs.microsoft.com/en-us/dotnet/api/system.collections.arraylist?view=netframework-4.8

如果将以上内容替换为List<Account>,一切都会变得更好:

    static void Main(string[] args)
    {
        List<Account> listOfAccounts = new List<Account>();
        double amt = double.Parse("133.1");
        listOfAccounts.Add(new Account(amt));
        foreach(Account account in listOfAccounts)
        {
            Console.WriteLine(account.balance.ToString());
        }
        Console.ReadLine();
    }