Search code examples
c#clientbotsirc

I have a listbox of ports, I keep getting "Cannot implicitly convert type 'int' to 'string'"


I'm getting "Cannot implicitly convert type 'int' to 'string" on this loading method This is the loaded when the options page is viewed:

private void Options_Load(object sender, EventArgs e)
{
    txtbBotUsername.Text = Properties.Settings.Default.setting_bot_username;
    txtbBotOAuth.Text = Properties.Settings.Default.setting_bot_oauth_password;
    lstBotPort.Text = Properties.Settings.Default.setting_bot_port; // Error Here
    txtbStreamName.Text = Properties.Settings.Default.setting_stream_name;
    txtbStreamerUsername.Text = Properties.Settings.Default.setting_streamer_username;
    txtbStreamerOAuth.Text = Properties.Settings.Default.setting_streamer_oauth_password;

    chbStartWin.Checked = Properties.Settings.Default.setting_start_windows;
    chbStartMini.Checked = Properties.Settings.Default.setting_start_mini;
    chbCheckUpdates.Checked = Properties.Settings.Default.setting_check_updates_launch;
    chbMiniTray.Checked = Properties.Settings.Default.setting_mini_tray;
    chbCloseTray.Checked = Properties.Settings.Default.setting_close_tray;
    chbAlwaysTop.Checked = Properties.Settings.Default.setting_always_top;
}

I'm getting "Cannot implicitly convert type 'string' to 'int" on this saving method This is when the save button is clicked.

private void btnOptionsSave_Click(object sender, EventArgs e)
{
    Properties.Settings.Default.setting_bot_username = txtbBotUsername.Text;
    Properties.Settings.Default.setting_bot_oauth_password = txtbBotOAuth.Text;
    Properties.Settings.Default.setting_bot_port = lstBotPort.Text;
    Properties.Settings.Default.setting_stream_name = txtbStreamName.Text;
    Properties.Settings.Default.setting_streamer_username = txtbStreamerUsername.Text;
    Properties.Settings.Default.setting_streamer_oauth_password = txtbStreamerOAuth.Text;

    Properties.Settings.Default.setting_start_windows = chbStartWin.Checked;
    Properties.Settings.Default.setting_start_mini = chbStartMini.Checked;
    Properties.Settings.Default.setting_check_updates_launch = chbCheckUpdates.Checked;
    Properties.Settings.Default.setting_mini_tray = chbMiniTray.Checked;
    Properties.Settings.Default.setting_close_tray = chbCloseTray.Checked;
    Properties.Settings.Default.setting_always_top = chbAlwaysTop.Checked;
    Properties.Settings.Default.Save();
    this.Close();
}

These settings as you can see are stored in the programs settings and the setting "setting_bot_port" is set as INT and has a default value of 6667.

On the designer I have 3 ports to choose from in the combo box list, those are 6667,443,80 The type needs to be INT because the IRC class will only take an INT for the port connection.

Any help would be greatly appreciated, I'm sure it's something stupid and simple I forgot.


Solution

  • The issue here is that you are trying to assign an integer to a string and vice versa. When you read the property value, use .ToString() when you assign it to your text box.

    myTextBox.Text = myProperties.Port.ToString();
    

    When you save the port number back to settings, you need to convert it.

    mySettings.Port = Convert.ToInt32(myTextBox.Text);