Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I'm pretty new to C# and I'm trying to do something but without much success. I am trying to use the class Point (the one with coordinates).

This is the code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace app2{
    class Program{
        static void Main(string[] args){
            Point p1 = new Point();
            p1.X = 7;
            p1.Y = 6;
            Console.WriteLine(p1.X);
            Console.WriteLine(p1.Y);
            Console.ReadLine();

The error is :

The type or namespace Point could not be found

I've been using this class in Java in a very similar manner, am I supposed to declare my own Point class/function which returns X and Y coordinates?

Tip: If you hover over the problematic Word and see a small blue Point, you can open a Context-Menu and get some Actions like the Namespace-Include etc. – Matthias Müller May 14, 2014 at 7:39

In the new window, type in the search bar 'System.Drawing'. Double click in the results found and click the button 'OK'.

Now, in your code, where the other using statements are declared, add the following line:

using System.Drawing;

Note: I noticed you're creating a new Console application. Be aware that, if you want to create a basic user interface, the System.Drawing.Point structure won't help you, because in a Console application you cannot interact with the user using buttons, labels and coordinates useful to place your UI items. In a Console application, you can interact only using plain text. If you want to create a simple application with a basic user interface, create instead a Windows Forms application.

Please add System.Windows.Point to your reply too, it is available since .Net 3.0 and is preferred over System.Drawing when used for non display purposes, i.e. calculations in services. msdn.microsoft.com/en-us/library/… – user3285954 May 20, 2015 at 15:07

The Point structure is a part of the System.Drawing namespace.
You can either reference this namespace or create your own Point structure, which, if you only need it as a coordinate container could be a very simple structure.

Something like:

public struct Point 
   public int X {get;set;}
   public int Y {get;set;}

Altho, the original Point struct in the System.Drawing namespace got a few more functions than the above code (which obviously don't have any, hehe). If you need those, I would recommend including the System.Drawing namespace instead of making your own, cause it would probably take more time than you wish that it took for such a simple structure.

http://msdn.microsoft.com/library/system.drawing.point.aspx

If you just want to use a simple structure for 2 ints, you can use the Tuple class (from the System namespace).

Tuple<int, int> point = new Tuple<int, int>(2, 5);

In time: As stated by @Aaron Hudon on comments, be aware that a Tuple is a class (so, by using it you'll be having an instance's reference) while a Point is a struct. Thanks for the observation Aaron Hudon.

No intention on implying that Tuple and Point are completely similar. Just pointing out that if the OP's need is for something simple that can hold 2 int values, the Tuple instance could be used in a very straightforward manner. But it is in fact important to point out the difference, so I'm including your observation on the suggestion posted. Tks. – Marcelo Myara Jun 2, 2019 at 12:11

Move your cursor to the Point in your code and use the swift keyboard shortcut: Alt+Shift+F10. In there, you can easily attach the necessary namespace - in your case, it will be System.Drawing.

With that shortcut you can decide if you want to attach the whole namespace, or just use it localy, like SystemDrawing.Point _Point = new SystemDrawing.Point();

Bear in mind that you may have to add the library reference - you use a console program, which doesn't have Windows Forms .dll files (as it would be unwise to attach all the unnecessary libraries). You have to add one manually.

Go to you project in solution explorer, right click on References, add reference, Assemblies and then check the System.Drawing. Ounce you add that and the correct namespace (using System.Drawing), it will work.

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.