-2

I have list of objects and when I loop thought it and I want to print things into console. There is just same Author name in every object. When I go with breakpoints slowly throught every line It will print alright. I don't see logic why this happens. I have tried restarting visual studio etc. Nothing helped. Maybe is problem with that random generation? I don't know where then

Picture of how it is with breakpoints and without

CODE:

class Sentences
    {
        private int id;
        private string sentence;
        private string author;

        public Sentences(int ID, string Sentence, string Author)
        {
            this.id = ID;
            this.sentence = Sentence;
            this.author = Author;
        }

        public int ID
        {
            get { return id; }
            set { id = value; }
        }

        public string Sentence
        {
            get { return sentence; }
            set { sentence = value; }
        }
        public string Author
        {
            get { return author; }
            set { author = value; }
        }

    }

Main console app:

  class Program
    {
      

        static void Main(string[] args)
        {
           
            List<Sentences> Sentences_list = new List<Sentences>(); 

            string sentences = DownloadString("http://mvi.mechatronika.cool/sites/default/files/berces.html"); 
            string[] partsFromString = sentences.Split(new string[] { @"." }, StringSplitOptions.None);
           

            for(int i = 0 ; i < partsFromString.Length - 1 ; i++)
            {                
                Sentences_list.Add(new Sentences(i+1,partsFromString[i],RandomString()));            
            }
            var d = Sentences_list;

            while (true)
            {
                string selected_from_menu = Menu();

                if (selected_from_menu == "1")
                {
                    Console.Clear();

                    foreach (Sentences s in Sentences_list)
                    {
                        Console.WriteLine("Autor: " + s.Author);
                        Console.WriteLine("ID: " + s.ID);
                        Console.WriteLine("Věta: " + s.Sentence);
                        Console.WriteLine("--------------------------------------------------");
                        Console.ReadLine();
                    }
                }            
            }
              
            
                

       }





        public static string Menu()
        {
            Console.WriteLine("1 - Výpis všetkých záznamov");
            Console.WriteLine("2 - Zmazanie záznamu podľa výberu užívateľa");
            Console.WriteLine("3 - Editovanie záznamu a autora vety podľa v´beru užívateľa");
            Console.WriteLine("4 - Pridanie záznamov");
            Console.Write("Vyberte možnost: ");
            string selected = Console.ReadLine();
          return selected;
        }
    public static string RandomString()
    {
        var chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        var stringChars = new char[4];
        Random random = new Random();

        for (int i = 0; i < stringChars.Length; i++)
        {
            stringChars[i] = chars[random.Next(chars.Length)];
        }
   
       return new String(stringChars);
    }

  

}
SPIRINN
  • 61
  • 6
  • 1
    Move `Random random = new Random()` outside of the method, if you read the documentation on Random you will see why – TheGeneral Feb 24 '21 at 21:27
  • "*On the .NET Framework, initializing two random number generators in a tight loop or in rapid succession creates two random number generators that can produce identical sequences of random numbers. ... Both to improve performance and to avoid inadvertently creating separate random number generators that generate identical numeric sequences, we recommend that you create one Random object to generate many random numbers over time.....*" – TheGeneral Feb 24 '21 at 21:31
  • Thanks a lot :) It is working now. love you – SPIRINN Feb 24 '21 at 21:35

2 Answers2

0

If DownloadString is asyncronous then the information hasn't had enough time to load before the rest of the code iterates through an empty list.

Minho17
  • 44
  • 6
0

Try to change your code to this:

private static Random random = new Random();

public static string RandomString(Random random)
{
    var chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    var stringChars = new char[4];
 
    for (int i = 0; i < stringChars.Length; i++)
    {
       tringChars[i] = chars[random.Next(chars.Length)];
    }

   return new String(stringChars);
}

...

for(int i = 0 ; i < partsFromString.Length - 1 ; i++)
{                
    Sentences_list.Add(new Sentences(i+1,partsFromString[i],RandomString(random)));            
}
TheGeneral
  • 79,002
  • 9
  • 103
  • 141
Serge
  • 40,935
  • 4
  • 18
  • 45