简体   繁体   中英

Point class in C#

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?

You should add a reference to your solution, to the namespace System.Drawing .

From the tab 'Solution Explorer', right click on 'References' and select 'Add Reference', as shown below:

SolutionExplorer

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.

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

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.

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.

你需要在顶部using System.Drawing添加。

Point类位于System.Drawing ,所以在顶部你应该写

using System.Drawing

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM