0

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

Capser
  • 3
  • 2
  • Why do you need this? You can create [class](https://learn.microsoft.com/en-us/dotnet/csharp/fundamentals/types/classes) with list, and two methods (to get all items and to add items). If you need something else, please add more information. – Lazar Đorđević Nov 20 '22 at 18:46
  • I see your edit, but I have same question. Why you must initialize your list in `Zaposlenici`, why not outside? – Lazar Đorđević Nov 20 '22 at 18:52
  • 1
    Im doing a task in which it says that i have to do initialize my list in Zaposlenici – Capser Nov 20 '22 at 18:57
  • I don't see where you call your `Zaposlenici`? If you want to do that from your main, just create a void list in `Main` function, then call `Zapolenici(List imena)` to fill the list, then you can after call another function with the same list in argument. But if the functions are in the same class (or same file), you can create a `public List imena` that will be available from any function, even better. – Siegfried.V Nov 20 '22 at 19:05

2 Answers2

1

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();
            }
        }
    }
Siegfried.V
  • 1,508
  • 1
  • 16
  • 34
1

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:

Demo

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:

Demo

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";
}
aloisdg
  • 22,270
  • 6
  • 85
  • 105
  • 1
    why do you use `IEnumerable` here? Also don't know th `Dump()` part? – Siegfried.V Nov 20 '22 at 19:15
  • `Dump()` is a common utility. You can find it in a lot of tool like linqpad or dotnetfiddle. I replaced it though. – aloisdg Nov 20 '22 at 19:16
  • 2
    `IEnumerable` describes behavior, while List is an implementation of that behavior. When you use `IEnumerable`, you give the compiler a chance to defer work until later, possibly optimizing along the way. If you want a list, you can use ` ToList()` to convert it. ([source](https://stackoverflow.com/questions/3628425/ienumerable-vs-list-what-to-use-how-do-they-work)) – aloisdg Nov 20 '22 at 19:18
  • Just interested, I am looking for tutorial about Dump(), but couldn't find some. IEnumerable not the first time I read about it, but never took time to look better, as I don't see its advantage (but I saw lot of people "high ranked" here use them often) – Siegfried.V Nov 20 '22 at 19:22
  • 2
    You should use the right tool for the right job. IEnumerable is often enough for what you want to do. Read about it. It is a great way to improve your code. About Dump: https://stackoverflow.com/questions/2699466/how-do-i-use-the-linqpad-dump-extension-method-in-visual-studio – aloisdg Nov 20 '22 at 19:25
  • I am reading about it right now, thanks. In fact, I am on a project, and now I am in a phase where I try to improve some parts of my code (I almost us only List or ObserbableCollection for binding) – Siegfried.V Nov 20 '22 at 19:30