Visual C# 2008 : Non-invocable member 'Microsoft.VisualBasic.Devices.Ports.SerialPortNames' cannot be used like a method

StackOverflow https://stackoverflow.com/questions/16154484

Question

can someone help me with this problem..? got this error when try debugging the code..

        private void Form2_Load(object sender, System.EventArgs e)
    {
        this.Show();
        Form1.DefaultInstance.Close();
        ToolTip1.SetToolTip(ComboBox1, "Please enter a VALID phone number");
        ToolTip1.SetToolTip(ComboBox2, "Please check your COM port number before selecting. Connection could be made for outgoing cable or bluetooth port with data calling supported phone.");
        ComboBox1.SelectedIndex = 0;

        for (int i = 0; i < My.Computer.Ports.SerialPortNames.Count; i++)
        {
            ComboBox2.Items.Add(My.Computer.Ports.SerialPortNames(i));
        }

        ComboBox2.SelectedIndex = 0;
    }

this happen at "SerialPortNames" in this line :

ComboBox2.Items.Add(My.Computer.Ports.SerialPortNames(i));
Était-ce utile?

La solution

You probably want to use indexer - [] instead of method call ()

  for (int i = 0; i < My.Computer.Ports.SerialPortNames.Count; i++)
  {
        ComboBox2.Items.Add(My.Computer.Ports.SerialPortNames[i]);
  }

Autres conseils

Perhaps try

For Each sp As String In My.Computer.Ports.SerialPortNames
    ListBox1.Items.Add(sp)
Next 

This was taken from http://msdn.microsoft.com/en-us/library/yfbcbt43(v=vs.90).aspx

You're using it like a method rather than a collection (following it with (i)).

Try one of these two:

for (int i = 0; i < My.Computer.Ports.SerialPortNames.Count; i++)
{
  ComboBox2.Items.Add(My.Computer.Ports.SerialPortNames[i]);
}

or

foreach(var portname in My.Computer.Ports.SerialPortNames)
{
  ComboBox2.Items.Add(portname);
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top