Share via


Introduction to HealthVault Development #5 – Do I weigh too much?

 

A common question asked by people is “do I weigh too much”, so we’re going to add the calculation of BMI to our application.

<aside>

The question of what a person’s desirable weight is – or even if there is a desirable weight – is one with lots of opinions. I chose to do BMI here because it’s simple and it works well, but it’s well known that BMI doesn’t work that well for a lot of people.

For example, my BMI (at 6’ 2” and 170 pounds) is 21.8, which puts me in the middle of the normal range. I would still be normal (at least according to the BMI ranges) even if I weighed 145 pounds, but I’m pretty gaunt if I get down to 165.

</aside>

The formula for BMI is quite simple (which is one reason it’s used so often):

BMI = weight (Kg) / (height (m) ^ 2);

Let’s write some code in Default.aspx.cs.

You will remember that when we configured our application, we asked for access to read the Height values, so we won’t have to update the configuration this time.

Last time we fetched values, we did the whole bit of creating the HealthRecordSearcher and the HealthRecordFilter, but this time we’re going to use a helper method to simplify the code:

Height height = GetSingleValue<Height>(Height.TypeId);
c_labelHeight.Text = height.Value.DisplayValue.ToString();

We also need to make the height value visible in the user interface. Flip in the designer for default.aspx, and set the visibility of the Height and c_labelHeight controls to true. Or, you can just edit the layout code directly if you wish.

Now, to add the BMI value to the table. We add a new header cell:

AddHeaderCells(c_tableWeight, "Date", "Weight", "BMI");

And we’ll add the code to calculate and display the bmi to our display loop:

double bmi = weight.Value.Kilograms / (height.Value.Meters * height.Value.Meters);
string bmiString = String.Format("{0:F2}", bmi);

AddCellsToTable(c_tableWeight, weight.When.ToString(),
                        weight.Value.DisplayValue.ToString(), bmiString);

Now, when we run the code, we’ll get our weight and BMI values. Or, it will blow up if you haven’t entered a height value yet. If that’s the case, you can enter one through the HealthVault Shell.

Here’s the full Page_Load() method:

protected void Page_Load(object sender, EventArgs e)
{
    HealthRecordSearcher searcher = PersonInfo.SelectedRecord.CreateSearcher();

    HealthRecordFilter filter = new HealthRecordFilter(Weight.TypeId);
    searcher.Filters.Add(filter);

    HealthRecordItemCollection weights = searcher.GetMatchingItems()[0];

    Height height = GetSingleValue<Height>(Height.TypeId);
    c_labelHeight.Text = height.Value.DisplayValue.ToString();

    AddHeaderCells(c_tableWeight, "Date", "Weight", "BMI");
    foreach (Weight weight in weights)
    {
        double bmi = weight.Value.Kilograms / (height.Value.Meters * height.Value.Meters);
        string bmiString = String.Format("{0:F2}", bmi);

        AddCellsToTable(c_tableWeight, weight.When.ToString(),
                        weight.Value.DisplayValue.ToString(), bmiString);
    }
}

Next Time

In the next episode, we’ll learn how to update data...

Comments

  • Anonymous
    January 30, 2009
    We are now ready to make our WeightTracker application do something useful. That something useful is

  • Anonymous
    March 06, 2009
    Welcome to the tutorial. In this tutorial, we will develop a HealthVault application from scratch. My