2

I'm fairly new to C#, just started 2 days ago because I need to create simple project for my Coding classes at University, unfortunately we had 3 days to complete the code so I'm 1 day behind, but it doesn't matter. I created a list of tuples using code fount @ whatacode.wordpress.com.

public class TupleList<int, string, string, int, int, string, int>
    : List<Tuple<int, string, string, int, int, string, int>>
{
    public void Add(int IDL, string AlbNmL, string ArtL, int RelDL, int TrAmnL, string LocL, int RatL)
    {
        Add(new Tuple<int, string, string, int, int, string, int>(ID, AlbNm, Art, RelD, TrAmn, Loc, Rat));
    }
}

I need to create list if it's first addition to tuple so I used if

if (currid == 0)
{
    var Albums = new TupleList<int, string, string, int, int, string, int>
    {
        {ID, AlbNm, Art, RelD, TrAmn, Loc, Rat},
    };
}

My ID, AlbNm, Art, RelD, TrAmn, Loc, Rat are result of readlines etc, doesn't really matter. (or does it??) I use currid as an indicator whether it's first or not (it start's with 0 and is ++ at the end of the add function.

Now my question is that how can I use the ADD method of my TupleList class in to add them (the ID, AlbNm, Art, RelD, TrAmn, Loc, Rat that I got from readlines) as a next tuple. I used

if(currid > 0) 

but I don't really know what to put into that if. Hope my question is understandable in any % and that someone can help me :) Thanks in advance.

8
  • 1
    Is your university actually making you use tuples or is there any reason why your not using a class? Commented May 21, 2014 at 9:59
  • Why not use a List<Tuple<..>>()? You get all the functions you need baked in. Commented May 21, 2014 at 10:00
  • I don't think your first snippet will compile as is. Have you tried it? Commented May 21, 2014 at 10:00
  • It does not compile for sure. Also, the author of that blog is, shall we say, "not very experienced". Follow their advice at your own risk. Commented May 21, 2014 at 10:01
  • 3
    Hmm. I think it'd be useful if you told us what you're actually trying to achieve here. Whatever it is, the way you're going about it is almost certainly not the right way :) Commented May 21, 2014 at 10:03

3 Answers 3

3

Would be much better to create an album class and make a list of albums

public class Album
{
    public string Name {get;set;}
    public string Artist {get; set;}
    public Album(string _name, string _artist)
    {
         Name = _name;
         Artist = _artist;
    }
}


Album example = new Album("a", "good idea");

List<Album> listOfAlbums = new List<Album>();
listOfAlbums.Add(example);
Sign up to request clarification or add additional context in comments.

8 Comments

I need to create the whole db in console so I think constructors are not the way to go.
Fml, I just figured it out how to create that thing, will work hard on it thanks :)
@user3658127 - As Matt has said, this is entirely a starting point but there are things such as xml serialization that could make populating your list very easy
I don't understand what you mean? If your using a console to ask a user for album details then you just take them and pass them to a new instance of an Album? (i.e the 3rd line from bottom in the code example after the equals sign)
That's the answer. Thank you very much.
|
2

First, you can just use a List<Tuple<int, string, string, int, int, string, int>>

Second, Your list is only in scope within the curly braces { } of the if (currid == 0) statement. That means it does not exist outside there, so you need to declare it outside of the if. Then, you can use Add. But also note that Tuple has a Factory method:

List<Tuple<<int, string, string, int, int, string, int>> Albums;
if (currid == 0) {
    Albums = new TupleList<int, string, string, int, int, string, int>();
}
Albums.Add(Tuple.Create(ID, AlbNm, Art, RelD, TrAmn, Loc, Rat));

Comments

0

Posting this in case it helps someone, as other replies are not exactly helpful and are nearly 10 years old.

//...
var candidateMoves = new List<(Move move, bool isSafe, int pointBenefit)>(); // tuple list definition
candidateMoves.Add(
            (move: m, isSafe: false, pointBenefit: 0)
            );

The trick is that you have to specify each "field" of tuple with a colon sign within the parenthesis.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.