Easy
Easy

Reputation: 127

C# adding to list of tuples with a function

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.

Upvotes: 2

Views: 12673

Answers (3)

Emre Bener
Emre Bener

Reputation: 1412

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.

Upvotes: 0

Dennis_E
Dennis_E

Reputation: 8894

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

Upvotes: 2

Sayse
Sayse

Reputation: 43300

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

Upvotes: 3

Related Questions