Vasile Laur
Vasile Laur

Reputation: 695

Update ProgressBar with appropriate value

Not strong in math so here is my problem:

I am using a progress bar to show progress of work performed in background:

Snippet of my code:

            int i = 0;
            int totalFriends = 0;

            foreach (dynamic friend in facebookFriends)
            {
                totalFriends++;
            }

            foreach (dynamic friend in facebookFriends)
            {
                i++;

                var friend = new FacebookFriend
                {
                    FbId = friend["uid"].ToString()
                };

                AccountFacebookFriendRepository.SaveOrUpdate(accountFriend);
            }

Now the application does much more than that and here I am only performing a small chunk of work: So for example before I reached this section the progress bar had value of 7 and after performing the work it has to reach to 20 and I want to update it while doing the work with appropriate values from 7 to 20:

My take on this is the following:

var intiProgressbarValue = 7;
var finalProgressbarvalue = 20;

foreach (dynamic friend in facebookFriends)
{
    i++;
    var friend = new FacebookFriend
                     {
                         FbId = friend["uid"].ToString()
                     };
    AccountFacebookFriendRepository.SaveOrUpdate(accountFriend);
    var calculatedValue = CalculatedValue(initProgressbarValue, finalProgressBarValue,totalFriends, i);
    UpdateProgressBar( calculatedValue);
}
//note that totalFriends can be any number lets say from 0 to 5000
private int CalculatedValue(int initVal, int finalVal, int totalFriends, int currentFriend)
{
    int progressBarVal = 0;
    //** 
       Perform logic so it will return a progress value that is bigger that 7 and smaller that 20 depending on the number of friends and currently updated friend
    **//
    progressBarVal  = 8;//this would be the result of calculation, a value from 8 to 20
    return progressBarVal;
}

Any help greatly appreciated:

Upvotes: 4

Views: 260

Answers (2)

Adam Liss
Adam Liss

Reputation: 48290

You can use the formula

progressBarVal = initVal + (finalVal - initVal) * (currentFriend/totalFriends);

To check the math, calculate progressBarVal when currentFriend is 0:

initVal + (finalVal - initVal) * 0 = initVal

and when currentFriend is totalFriends:

initVal + (finalVal - initVal) * 1 = finalVal

Upvotes: 3

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726569

Try this:

private int CalculatedValue(int initVal, int finalVal, int totalFriends, int currentFriend)
{
    initVal++;
    var diff = finalVal - initVal; // 20-8 = 12
    return (diff*(currentFriend+1))/totalFriends + initVal;
}

This assumes that currentFriend changes from 0 to totalFriends-1, inclusive. For example, if currentFriend = 99 and totalFriends = 300, the answer this function returns is 12, one third through the range of 8..20 (inclusive).

Upvotes: 3

Related Questions