C
C#2y ago
DiMiaN

✅ How to add data to a list that is outside of Main()?

using System.Collections.Generic;
internal class Program
{
public static void insertData(string animal, int amount)
{
var list = new List<KeyValuePair<string, int>>();
list.Add(new KeyValuePair<string, int>(animal, amount));
}
private static void Main(string[] args)
{
insertData("Dog", 4);
insertData("Cat", 12);

foreach(var element in list)
{
Console.WriteLine(element);
}
}
}
using System.Collections.Generic;
internal class Program
{
public static void insertData(string animal, int amount)
{
var list = new List<KeyValuePair<string, int>>();
list.Add(new KeyValuePair<string, int>(animal, amount));
}
private static void Main(string[] args)
{
insertData("Dog", 4);
insertData("Cat", 12);

foreach(var element in list)
{
Console.WriteLine(element);
}
}
}
I know there are some errors but I can't figure out how to solve this. Also when calling the insertData the list will be resetted every new call?
13 Replies
VydrOz
VydrOz2y ago
yes cause you declare it in the method insertData, So list is not accessible in main you must instantiate your list variable externally OR pass it directly into the method and return it / using reference
MODiX
MODiX2y ago
RubyNova#0404
if you're unsure of by ref. vs. by val. look at this gif until it makes sense: https://media.giphy.com/media/xUPGcLrX5NQgooYcG4/giphy.gif
Embed Type
Gifv
Quoted by
<@!358653201513447465> from #chat (click here)
From RubyNova#0404
React with ❌ to remove this embed.
DiMiaN
DiMiaNOP2y ago
geez I dont even know what the word instantiate means. I dont even know where to begin
VydrOz
VydrOz2y ago
declaration : List<KeyValuePair<string, int>> list; instantiation : list = new List<KeyValuePair<string, int>>();
Declaring a variable means to introduce a new variable to the program. You define its type and its name. Instantiate - Instantiating a class means to create a new instance of the class.
VydrOz
VydrOz2y ago
Stack Overflow
Meanings of declaring, instantiating, initializing and assigning an...
Technically what are the meanings and differences of the terms declaring, instantiating, initializing and assigning an object in C#? I think I know the meaning of assigning but I have no formal
DiMiaN
DiMiaNOP2y ago
thanks, appreciated. now I follow 🙂
VydrOz
VydrOz2y ago
feelsniceman np
DiMiaN
DiMiaNOP2y ago
using System.Collections.Generic;
internal class Program
{
private static List<Animals> animalList = new List<Animals>();
public class Animals
{
public string Name { get; set; }
public int Amount { get; set; }

public Animals(string Name, int Amount)
{
this.Name = Name;
this.Amount = Amount;
}
}
public static void insertData(string animal, int amount)
{
animalList.Add(new Animals(animal, amount));
}
private static void Main(string[] args)
{
insertData("Dog", 4);
insertData("Cat", 12);

foreach (Animals animals in animalList)
{
Console.WriteLine(animals.Name);
}
}
}
using System.Collections.Generic;
internal class Program
{
private static List<Animals> animalList = new List<Animals>();
public class Animals
{
public string Name { get; set; }
public int Amount { get; set; }

public Animals(string Name, int Amount)
{
this.Name = Name;
this.Amount = Amount;
}
}
public static void insertData(string animal, int amount)
{
animalList.Add(new Animals(animal, amount));
}
private static void Main(string[] args)
{
insertData("Dog", 4);
insertData("Cat", 12);

foreach (Animals animals in animalList)
{
Console.WriteLine(animals.Name);
}
}
}
Am I even doing something right here or?
VydrOz
VydrOz2y ago
you need to add using System; to call Console.WriteLine() otherwise the code seems correct i guess you are learning to code, but if you put everything in the Program.cs, it is a good practice to put the main method on top
DiMiaN
DiMiaNOP2y ago
yeah Im a beginner. Ive worked a little bit with python and php before
VydrOz
VydrOz2y ago
and separate the class Animal (without "s") in an other file
DiMiaN
DiMiaNOP2y ago
alright, thanks alot man I will close this if there is nothing more to add
VydrOz
VydrOz2y ago
yep OkCursor
Want results from more Discord servers?
Add your server