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:
Try this:
This assumes that
currentFriendchanges from 0 to totalFriends-1, inclusive. For example, ifcurrentFriend = 99andtotalFriends = 300, the answer this function returns is 12, one third through the range of 8..20 (inclusive).