简体   繁体   English

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

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

I am working on a web form that takes the input of information and stores it to a dictionary, then adds it to a listbox. 我正在处理一个Web表单,该表单接受信息的输入并将其存储到字典中,然后将其添加到列表框中。

newAccount.ID is set as an integer inside the Account class. newAccount.ID在Account类中设置为整数。 And I have parsed it in the click event. 我已经在click事件中对其进行了解析。 However, it says "Argument 1: cannot convert from 'int' to 'string'. I am currently checking to see if the dictionary contains the account ID before adding an exception. 但是,它说“参数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))

    }

I am guessing your dictionary key is string not integer . 我猜你的dictionary键是string而不是integer First of all that is not the best, as the data you want to keep there is of type int 首先,这不是最好的,因为要保留的数据类型为int

If you want to keep that though: 如果您想保留该内容:

newAccount.ID.ToString();

Will do the trick. 会成功的。

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

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