Click here to Skip to main content
15,891,896 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
I have another public class in my wpf project and I want to return the value from the loop on the fly. I don't want to do it after the loop is completed

What I have tried:

C#
public MainWindow()
{
   void ShowNumbers()
{
   foreach (var item in Numbers.Number.Generate)
  {
  txtbox1.Text = item;
  }
 }
}
public class Numbers
{
  public Number()
   {
      for (int i = 0, i < 100000, i++)
       {
         return i;
       }
    }
}
}
Posted
Updated 25-May-16 4:13am
v2
Comments
Kenneth Haugland 24-May-16 7:41am    
You need Events for that, or Rx, or just a delegate; it depends on what you want. Have a look at this:
http://www.codeproject.com/Articles/1061085/Delegates-Multicast-delegates-and-Events-in-Csharp

The video from his article gives a nice explanation:
https://www.youtube.com/embed/ifbYA8hyvjc
Philippe Mori 25-May-16 10:16am    
By the way, your code does not compile...
Philippe Mori 25-May-16 10:22am    
Why would you update the same textbox 100000 times. User would only see final result and it would waste CPU time!

Clearly, that question need to be improved a lot!

A bit of a crazy idea, but yo may like to look into using yield (C# Reference)[^].
 
Share this answer
 
Comments
Philippe Mori 25-May-16 9:49am    
Not crazy at all... This is probably what OP want to do and the best way to do it.
Don't know what you want to do, but this code
C#
public Number()
{
   for (int i = 0, i < 100000, i++)
    {
      return i;
    }
}

can be simplified to
C#
public Number()
{
    return 0;
}
 
Share this answer
 
Comments
Richard MacCutchan 24-May-16 11:09am    
Well spotted.
Philippe Mori 25-May-16 9:54am    
Also constructor name does not match class name and a constructor cannot return a value.
Patrice T 25-May-16 10:27am    
I was just reporting 1 problem.
I don't really now what he want to do with a constructor in a class that have nothing to construct.
Philippe Mori 25-May-16 10:46am    
This is just a comment for completeness to a good answer.
You could try creating an event that fires every iteration and then handling that in the 'listening' class.

Just make sure that if you're updated the UI you do so on the dispatcher thread or you'll get errors.
 
Share this answer
 
v2
Comments
Philippe Mori 25-May-16 9:50am    
In a case like this, it is probably far more easier and adequate to create an enumerator as mentioned in solution 1.
As mentioned in solution 1, the easiest way to do it would be to use yield operator.

As mentioned in solution 3 and in commented code below, there are a lot of possible improvements that should be made for code. In fact, your code would not even compile.

But since Linq already have an extension method for that, it would be even simpler to reuse that instead of reinventing the wheel.

So something like:
C#
// Ensure that Linq is included...
using System.Linq;

// Add missing class keyword...
public class MainWindow()
{
   // Class constant should be declared as such. Avoid magic numbers in code...
   private const int itemCount = 100000;

   // It is a ggod habit to always specify access (here private)...
   private void ShowNumbers()
   {
      foreach (var item in Enumerable.Range(0, itemCount))
      {
         // By the way, you should give a better name to your 
         // text box variable...
         // You also need to convert your number to a string
         // and apply desired formatting.

         // By the way, it make no sense to update same control text 
         // multiple time in a loop as only last value would be visible!
         txtbox1.Text = item.ToString("N0");

         // Thus you maybe want a combobox instead...
         // We cannot guess from your question!
         // This is WinForms syntax. I don't know WPF syntax by heart.
         comboBox.Items.Add(item.ToString("N0"));
      }
   }
}

Alternatively, if you want your own enumerator (maybe you want something more complex), then the following code would do:
C#
class Numbers
{
   public IEnummerable<int> MyList 
   {
      get 
      {
         // This is just to get the idea. In production code, you would never
         // have hard-coded constants in your code, you would use a function
         // with parameters so that the code can be reused elsewhere...
         for (int i = 0; i < 100000; i++)
         {
            yield return i;
         }
      }
   }
}
 
Share this answer
 
v4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900