繁体   English   中英

从字符串转换为int以实现到Dictionary

[英]Converting from string to int for implementation into Dictionary

我正在处理一个Web表单,该表单接受信息的输入并将其存储到字典中,然后将其添加到列表框中。

newAccount.ID在Account类中设置为整数。 我已经在click事件中对其进行了解析。 但是,它说“参数1:无法从'int'转换为'string'。我目前正在检查在添加异常之前,字典是否包含帐户ID。

protected void btnAdd_Click(object sender, EventArgs e)
    {
        Account newAccount = new Account();

        newAccount.ID = int.Parse(txtID.Text);
        newAccount.Balance = double.Parse(txtBalance.Text);
        newAccount.Name = txtName.Text;

        if (myAccounts.ContainsKey(newAccount.ID))

    }

我猜你的dictionary键是string而不是integer 首先,这不是最好的,因为要保留的数据类型为int

如果您想保留该内容:

newAccount.ID.ToString();

会成功的。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM