i am trying to make a program in which you have a list of workers,where you can add new workers and delete workers. I made a function which contained 5 names. i then want to make a second function to add 2 new names to the 1. function.
public static void Zaposlenici()
{
List<string> imena = new List<string> { "Marko","Ivan","Miljenko","Josip","Luka"};
foreach (var ime in imena)
{
Console.WriteLine(ime);
}
}
public static void Izbornik()
{
Console.WriteLine("1. Zaposlenici u firmi");
Console.WriteLine("2. Dodaj novog zaposlenika");
Console.WriteLine("3. Izbrisite zaposlenika");
Console.WriteLine("0. Izlaz");
Console.WriteLine("--------------------");
Console.WriteLine("");
Console.WriteLine("Odaberite opciju: ");
}
public static void DodajZaposlenika()
{
List<string> NovaImena = new List<string> { "Francis", "Matea" };
}
public static void Opcije()
{
int opcija= Int32.Parse(Console.ReadLine());
switch (opcija)
{
case 1:
Zaposlenici();
break;
default:
break;
}
}
static void Main(string[] args)
{
Console.WriteLine("Pozdrav!");
Console.WriteLine("---------------");
Izbornik();
Opcije();
}
}
I simply tried using the 1. function in the 2. so i thought i could just change it but i cant seem to be able to use the contents from the 1. function
CodePudding user response:
You can easily modify your function's list, passing the list in argument :
public class yourWindow : Window
{
public void yourFunction1()
{
List<string> imena = new List<string> { "Marko","Ivan","Miljenko","Josip","Luka"};
yourFunction2(imena);
}
public void yourFunction2(List<string> imena)
{
imena.Add("Aleksey");
}
This will then add it to your list.
Edit : you also can create a list (public or private) that will be available from any function :
public class YourMainWindow : Window //you have something like that
private imena{get;set;}=new List<string>();
public static void Zaposlenici()
{
this.imena = new List<string> { "Marko","Ivan","Miljenko","Josip","Luka"};
foreach (var ime in imena)
{
Console.WriteLine(ime);
}
}
public static void Izbornik()
{
Console.WriteLine("1. Zaposlenici u firmi");
Console.WriteLine("2. Dodaj novog zaposlenika");
Console.WriteLine("3. Izbrisite zaposlenika");
Console.WriteLine("0. Izlaz");
Console.WriteLine("--------------------");
Console.WriteLine("");
Console.WriteLine("Odaberite opciju: ");
}
public static void DodajZaposlenika()
{
this.imena.AddRange(new List<string> { "Francis", "Matea" });
}
public static void Opcije()
{
int opcija= Int32.Parse(Console.ReadLine());
switch (opcija)
{
case 1:
Zaposlenici();
break;
default:
break;
}
}
static void Main(string[] args)
{
Console.WriteLine("Pozdrav!");
Console.WriteLine("---------------");
Izbornik();
Opcije();
}
}
}
CodePudding user response:
While you can easily modify your function's list. You should not though. No really don't mutate object parameter. It is not a good practice. Here is a solution which did not mutate the list:
public static void Main()
{
yourFunction1();
}
public static void yourFunction1()
{
var imena = new List<string>{"Marko", "Ivan", "Miljenko", "Josip", "Luka"};
var imena2 = yourFunction2(imena);
Console.WriteLine(string.Join("\n", imena2));
}
public static IEnumerable<string> yourFunction2(IEnumerable<string> imena)
{
return imena.Append("Francis").Append("Matea");
}
another solution would be to use yield return
and concat:
public static void yourFunction1()
{
var imena = new List<string>{"Marko", "Ivan", "Miljenko", "Josip", "Luka"};
var imena2 = imena.Concat(yourFunction2());
Console.WriteLine(string.Join("\n", imena2));
}
public static IEnumerable<string> yourFunction2()
{
yield return "Francis";
yield return "Matea";
}