-2

I'm trying loop through and change the Console.BackgroundColor for each item in a list, and am wondering how I would go about doing so.

public static List<int> myList = new List<int>();

foreach(//loop through the list){
  Console.BackgroundColor = //set background color for each item in myList
}

otisjnz
  • 136
  • 1
  • 15
  • Why not use an index or Dictionary? – Ray May 12 '22 at 22:58
  • How woudl I go about doing that? – otisjnz May 12 '22 at 23:01
  • The documentation on Console.BackgroundColor actually has an [example](https://learn.microsoft.com/en-us/dotnet/api/system.console.backgroundcolor?view=net-6.0#examples) on how to loop trough all the available colors – GuyVdN May 12 '22 at 23:01
  • Does this answer your question? [using foreach loop for two lists in c# for geting lists values](https://stackoverflow.com/questions/11410593/using-foreach-loop-for-two-lists-in-c-sharp-for-geting-lists-values) – 41686d6564 stands w. Palestine May 12 '22 at 23:04
  • Hmm, I'm going to try rephrase. I want to print out each item in cardTypeStore with each color from cardColorStore. Would you mind showing an example? – otisjnz May 12 '22 at 23:05
  • @41686d6564standsw.Palestine That should work for looping through two lists, I just don't know how to change the color for each one. – otisjnz May 12 '22 at 23:05
  • Why not use a for loop? It is better suited for this task. Otherwise you create an index and increase it at the end of the foreach loop. – hijinxbassist May 12 '22 at 23:17
  • Could you/someone give some sort of demo on how to do this? I'm pretty new to programming. – otisjnz May 13 '22 at 00:09
  • 2
    @ConsoleCreative Have you checked basic C# tutorials / books / courses? I'm sorry if this sounds kinda rude unintentionally, but given your comments on this and the answer you miss some knowledge fuel to get up to speed to go anywhere. – Ray May 13 '22 at 08:27
  • I'll make sure to learn more about how the language works, and also OOP. Thanks – otisjnz May 13 '22 at 08:28
  • Best not to use this site in place of a basic language tutorial – Hovercraft Full Of Eels May 13 '22 at 10:38
  • This edit does not fix the question, because it invalidates the answer that was given, and because it is needlessly simplified ("how do I iterate" is so trivial that nobody ever saw fit to make a canonical for it, even though everyone needs to know). The original question should not have been closed as "needs more focus", because it is perfectly understandable, and a common problem with a canonical. Instead, it should have been closed as a duplicate of that canonical: [Iterate two Lists or Arrays with one ForEach statement in C#](https://stackoverflow.com/questions/1955766/). – Karl Knechtel Feb 04 '23 at 19:13

1 Answers1

0

Instead of using a foreach loop, you can use a for loop. The benefit of using the for loop is the index, which can be used to access both collections.

for (int i = 0; i < cardTypeStore.Count; i++)
{
    Console.BackgroundColor = cardColorStore[i];
    Console.Write(cardTypeStore[i]);
}

This will require both collections to have the same number of items, otherwise you risk an Index Out of Range Exception.

A better option, if possible, is to use a class to hold your data. If you want to make changes later, this is much easier to maintain.

public class CardData
{
    public string Type { get; set; }
    public int Color { get; set; }
}

Then you create a single list that you can iterate over using for or foreach.

var cardDataList = new List<CardData>();

// ... populate list with data

foreach (var cardData in cardDataList)
{
    Console.BackgroundColor = cardData.Color;
    Console.Write(cardData.Type);
}
hijinxbassist
  • 3,667
  • 1
  • 18
  • 23
  • One thing, I want to store the `var cardDataList = new List();` as a global variable, but it's giving me the error `The contextual keyword 'var' may only appear within a local variable declaration.` Would you know why by any chance? – otisjnz May 13 '22 at 00:44
  • For class level variable you cannot use the `var` keyword. Instead use the type. `List cardDataList = new List();` – hijinxbassist May 13 '22 at 00:47
  • That fixed it great! One other question (sorry for so many) I'm adding data to the old lists by going `cardTypeStore.add(//random int here)` and `cardColorStore.add(//random type here)`, but it's throwing `Argument 'number' cannot convert from TypeA to TypeB`. Any clue why? – otisjnz May 13 '22 at 00:52