0

I have the below code that gets the data as datatable in sub class.Then i sorted the data with some if conditions in the foreach statement.I need to inform the changes when it met the condition in if statement to the subscriber.But if one condion met in the foreach statement,it inform the subscriber.Then it doesn't return back to the loop after looping the first row .Any one know how to do this?

public class Data
{
    public string ID { get; set; }
    public string Description { get; set; }
}

public delegate void LogHandler(Data log);

public class ClsSub
{
    public event LogHandler OnDataRetrieved;

    public void LoadData()
    {
        DataTable dt = GetData();

        foreach (DataRow row in dt.Rows)
        {
            Data logdata = new Data();
              if (row["Description"].ToString().Contains("User found"))
                {
                 logdata.ID = row["UserID"].ToString();
                 logdata.Description = row["Description"].ToString();
                 }
               if (row["Description"].ToString().Contains("User not found"))
                {
                 logdata.ID = row["UserID"].ToString();
                 logdata.Description = row["Description"].ToString();
                 }

            if (OnDataRetrieved != null)
            {
                OnDataRetrieved(logdata);
            }
        }
    }

    private DataTable GetData()
    {
        var result = new DataTable();
        result.Columns.Add("UserID", typeof(string));
        result.Columns.Add("Description", typeof(string));
        result.Rows.Add("user1", "description1");
        result.Rows.Add("user2", "description2");
        return result;
    }
}

public class ClsMain
{
    public event LogHandler OnDataRetrieved;

    public void ReadData()
    {
        ClsSub sub = new ClsSub();
        sub.OnDataRetrieved += OnDataRetrieved;
        sub.LoadData();
    }
}

public class Program
{

    static void Main(string[] args)
    {
        ClsMain main = new ClsMain();
        main.OnDataRetrieved += ClsApplication_OnDataRetrieved;
        main.ReadData();
        Console.ReadKey();
    }

    private static void ClsApplication_OnDataRetrieved(Data log)
    {
        Console.WriteLine(log.ID + " " + log.Description);
    }
}
Michael
  • 43
  • 6
  • 2
    `.Then it doesn't return back to the loop after looping the first row` how have you proved this to your self, oh and us... loops just dont stop looping, unless there is nothing to loop or an exception is thrown, or you have forced it to do so – TheGeneral Apr 04 '19 at 02:52
  • @Michael Randall Yes after checking the first row ,it met the condition and publish the change.But it doesn't continue the loop. – Michael Apr 04 '19 at 02:55
  • What does _"But it doesn't continue the loop"_ mean? Does it just hang on a specific line of code, does it produce an exception (error)? Does it crash? ¿slǝɐɥɔᴉW oʍʇ ǝɹǝɥʇ ǝɹɐ ʎɥM – ProgrammingLlama Apr 04 '19 at 02:57
  • @MichaelRandall if (OnDataRetrieved != null) { OnDataRetrieved(logdata); } – Michael Apr 04 '19 at 02:57
  • @John No error occured.after publish the change and subscriber gets it.Then it dosn't continuing the loop. – Michael Apr 04 '19 at 03:00
  • @Michael So it hangs? Or does it exit the loop? – ProgrammingLlama Apr 04 '19 at 03:01
  • @John Yes it exits the loop.Please see the foreach statement and event publish method I had written.Is there any error? – Michael Apr 04 '19 at 03:02
  • @John Is there any way? – Michael Apr 04 '19 at 03:08
  • It seems that there's no issue with your loop. It's simply a case that you have no rows that match either of your if statements, so you're passing an empty object to event, which is printing a blank row. I [modified your data](https://rextester.com/WKCP49163) and it works. – ProgrammingLlama Apr 04 '19 at 03:53
  • Is there anything different between this question and [the one you asked yesterday](https://stackoverflow.com/questions/55497377/how-to-trigger-an-event-across-classes-using-delegates/55500197#55500197)? I could be wrong, but they look exactly the same. – Scott Hannen Apr 04 '19 at 18:31

0 Answers0