C# 2008 中的元组错误 [英] Error with Tuple in C# 2008

查看:34
本文介绍了C# 2008 中的元组错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 C# 2010 中编写了一个程序,我的代码包含一个元组,但是当我将程序放入 C# 2008 时,它无法识别它,并出现以下错误:

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:

找不到命名空间名称‘Tuple’的类型"

所以我不知道如何使这个工作,这是发生错误的代码行:

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>>();

请帮忙.

编辑

基本上这是我目前由于错误而无法编译的代码:

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;
    }
}

所以我需要一种方法来改变或使元组在 VS C# 2008 和 2010 中工作,

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

谢谢

推荐答案

Tuple 类不在 v4 之前的框架中,但这里有一个简化版本,可以满足您的大部分需求:

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);
    }
}

你可以轻松地添加类来拥有超过 2 个参数的元组

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

这篇关于C# 2008 中的元组错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆