WCoaster
WCoaster

Reputation: 39

Cant type System.IO.Ports shorthand

I have the following code but still have to type full path to use the System.IO.Ports namespace even though I have the using clause in place. Am I missing something in my reference list?

The = new SerialPort returns a Error 5 'SerialPort' is a 'namespace' but is used like a 'type'

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;

namespace SerialPort
{
    public partial class Form1 : Form
    {
        System.IO.Ports.SerialPort counter = new SerialPort("COM5");
        public Form1()
        {
            InitializeComponent();

        }

Thanks

Upvotes: 1

Views: 368

Answers (2)

Honza Brestan
Honza Brestan

Reputation: 10957

It's because your namespace carries the same name. Either rename your namespace or use an alias for the serial port, like this:

using SP = System.IO.Ports.SerialPort

And then you can use

SP counter = new SP("COM5");

But as Jon suggested, renaming your namespace is a clearer solution to anyone who'll read your code.

Upvotes: 2

Jon Skeet
Jon Skeet

Reputation: 1502376

You're declaring a namespace of SerialPort. Don't do that. That's what's causing the problem.

All you've got to do is change the namespace, and you'll be fine. You could use an alias as per Honza's request, but I think the code would be clearer to everyone if you just renamed the namespace.

Upvotes: 2

Related Questions