Error with Tuple in C# 2008

我的梦境 提交于 2019-12-09 14:10:55

问题


I have made a program in C# 2010 and my code contains a Tuple, but when I put my program into C# 2008 it does not recognise it, and comes up with the error of:

"The type of namespace name 'Tuple' could not be found"

So I don't know how to make this work, this is the line of code in which the error occurs:

private List<Tuple<Point, Point>> lines = new List<Tuple<Point, Point>>();

Please help.

EDIT

Basically this is my code at the moment which doesn't compile due to the error:

public partial class Form1 : Form
{
    private bool isMoving = false;
    private Point mouseDownPosition = Point.Empty;
    private Point mouseMovePosition = Point.Empty;
    private List<Tuple<Point, Point>> lines = new List<Tuple<Point, Point>>();
    public Form1()
    {
        InitializeComponent();
    }

    private void pictureBox1_Paint(object sender, PaintEventArgs e)
    {
        var g = e.Graphics;
        if (isMoving)
        {
            g.Clear(pictureBox1.BackColor);
            g.DrawLine(Pens.Black, mouseDownPosition, mouseMovePosition);
            foreach (var line in lines)
            {
                g.DrawLine(Pens.Black, line.Item1, line.Item2);
            }
        }
    }

    private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
    {
        isMoving = true;
        mouseDownPosition = e.Location;
    }

    private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
    {
        if (isMoving)
        {
            mouseMovePosition = e.Location;
            pictureBox1.Invalidate();
        }
    }

    private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
    {
        if (isMoving)
        {
            lines.Add(Tuple.Create(mouseDownPosition, mouseMovePosition));
        }
        isMoving = false;
    }
}

So I need a way of changing or making the Tuple work in VS C# 2008 as well as 2010,

Thanks


回答1:


The Tuple class isn't in pre-v4 framework, but here is a simplified version that should match most of your needs:

public class Tuple<T,U>
{
    public T Item1 { get; private set; }
    public U Item2 { get; private set; }

    public Tuple(T item1, U item2)
    {
        Item1 = item1;
        Item2 = item2;
    }
}

public static class Tuple
{
    public static Tuple<T, U> Create<T, U>(T item1, U item2)
    {
        return new Tuple<T, U>(item1, item2);
    }
}

you could easily add classes to have Tuples with more than 2 parameters




回答2:


Tuples are new in C# 4.0

Check the article linked to from this question, it explains their usage.

Will a future version of .NET support tuples in C#?




回答3:


Tuple is new in .NET Framework 4. Visual Studio only targets .NET Framework 3.5 as the latest version. Therefore, you are targetting a Framework that does not contain the Tuple class, and it won't compile.

If you really need it in Framework 3.5 / VS2008, it wouldn't be too hard to write your own Tuple class to make the existing code compile under that version (provided you are not using any other 4.0 specific stuff).




回答4:


Tuple is only available in .NET 4, which isn't supported by VS2008.




回答5:


Update to framework 4.0 (find any patch for VS 2008) else, visual studio 2010 is recommended for Tuple,



来源:https://stackoverflow.com/questions/4312218/error-with-tuple-in-c-sharp-2008

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!