GetPortNames problem

I have been trying to use the GetPortNames function to work in my C# project (I am a total newbie to C# and OOP) by copying code from an existing simple terminal program. However, no matter wherre I place the code, I get an error message:
"Static member 'System.IO.Ports.SerialPort.GetPortNames()' cannot be accessed with an instance reference; qualify it with a type name instead"
I have a serial port instance I've dragged onto my form, called serialPort1.
The code to get the port names is:
private void get_port_names()
{
string[] ports = serialPort1.GetPortNames();
comboCOM1.Items.Clear();
foreach (string s in ports)
comboCOM1.Items.Add(s);
}
This should generate a string array, and put the port names into the array. Then, the 'foreach' statement should load my combo box (comboCOM1) with the port values, but I can't seem to get past the error. I have read the online explanation of this error, but it is literally meaningless to me. The example project (which compiles and runs) has this code in the main() section, but no matter where I put in my project, it will not compile.
Please help! You can email me at dave.telling@mrgasket.com if you need to see more information.



Answer this question

GetPortNames problem

  • RayClark096

    I believe from that code, there is no method in the instantiated SerialPort object, the GetPortNames is a Static Method in the SerialPort Class

    Try this:

     



    this.theComboBox.Items.Clear();
    string[] thePortNames = System.IO.Ports.SerialPort.GetPortNames();
    foreach (string item in thePortNames)
    {
       this.theComboBox.Items.Add(item);
    }

     

     

    does this work for you



  • GetPortNames problem