Home get counter value int from method that return a bool and return to browser
Reply: 0

get counter value int from method that return a bool and return to browser

user9620
1#
user9620 Published in September 19, 2018, 11:36 am

I'm trying to work out how I can get the value of a counter in a method that is inside a foreach loop.

I have thought about delegates but I cannot work out how to do that.

My method is with all irreverent code removed

public static bool ReadExcelFile(string str1, int Id, out int tCount)
{
    int primaryCounter = 0;                    

    //Insert new items
    foreach (var item in excelList)
    {                
        primaryCounter++;                                
    }

    return true;
}                

I then have a ActionResults which I use for the jquery polling

public ActionResult InsertPolling()
{
    int currentCount = //How to get value of counter        
    string pollingMessage = $"Inserted {currentCount}  items";
    return Json(pollingMessage, JsonRequestBehavior.AllowGet);
}

My jQuery is:

function doPollHandler(event) {
    $.post('/UploadData/InsertPolling',
        function(data) {
            alert(data); // process results here
            setTimeout(window.doPollHandler, 2000);
        });
};

$('#fileUpload').change(function () {
    doPollHandler();
});

I have looked at https://www.tutorialspoint.com/csharp/csharp_delegates.htm

https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/delegates/using-delegates

https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/delegates/how-to-declare-instantiate-and-use-a-delegate

But as my method returns a bool, and I need to get the value of counter I'm stuck on how to do this.

share|improve this question
  • In the static method, I assume tCount and primaryCounter don't refer to the same thing? You could just add a second out parameter, but that starts to look smelly. We may need more details of the code to give a succinct answer – Rhumborl Apr 16 at 20:35
  • Hi @Rhumborl I cannot use tCount as that returns the count of all records at the end, what I am trying to do is get the value of primaryCounter every 2 seconds so that the visitor can see the progress. – Paul Thompson Apr 16 at 20:42
  • That's not what Rhumborl was saying. Rhumborl said you could add a second out parameter. And was hinting that if your method returns more than one thing, like it already does, consider returning a simple object with some properties to hold those values, rather than returning bool and having an out parameter. But you need the value updated before the method returns, so that won't help you. – Richardissimo Apr 16 at 22:18

1 Answer 1

active oldest votes
up vote 0 down vote accepted
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.318111 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO