1

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

Benjol
  • 63,995
  • 54
  • 186
  • 268
Chris Bacon
  • 995
  • 8
  • 30
  • 42
  • I think you need to ask another question: "How can I get Tuple to work in VS2008 and VS2010". I suspect that the answer is probably "you can't", or "why?!" – Benjol Nov 30 '10 at 10:26

5 Answers5

9

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

Dean Chalk
  • 20,076
  • 6
  • 59
  • 90
  • Exactly what I was going to suggest, note that you may run into namespace problems, and if you want to take your code back into 2010, you'll have conflicts too. (Also, this simplified implementation doesn't have all the functionality included in the .Net 4.0 version - it's not `IStructuralEquatable`, `IStructuralComparable` or `IComparable`) – Benjol Nov 30 '10 at 09:41
  • 1
    @Benjol: if it had *all* the functionality of the .NET 4 version, it would hardly be simplified ;) – Fredrik Mörk Nov 30 '10 at 09:44
  • and also you'd want to override Equals and GetHashCode - but its a good start if you dont want to upgrade to next version of framework – Dean Chalk Nov 30 '10 at 09:44
  • @Frederik, I know, but I thought it was important to highlight that in this specific case where the questioner is talking about existing code which was based on .Net 4. Going the other way wouldn't be so much of a problem. – Benjol Nov 30 '10 at 09:45
  • Ok, this is brilliant, how do i edit this code for my project, i need it to store it in the list 'lines', as it needs to store user drawn lines in this tuple called 'lines'? – Chris Bacon Nov 30 '10 at 09:46
  • @Chris - not sure what you're after but I guess something like List> lines = new ...... lines.Add(Tuple.Create(line,123)); – Dean Chalk Nov 30 '10 at 09:49
  • @Chris, it's difficult to know from your question. You need to declare this Tuple that @Dean has provided 'somewhere' in your code base where this calling code (and any other code using Tuple) can see it (i.e., in the same namespace). – Benjol Nov 30 '10 at 09:51
1

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#?

Community
  • 1
  • 1
Nostradamnit
  • 862
  • 1
  • 10
  • 20
0

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).

driis
  • 161,458
  • 45
  • 265
  • 341
0

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

Brian Rasmussen
  • 114,645
  • 34
  • 221
  • 317
0

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

PawanS
  • 7,033
  • 14
  • 43
  • 71