StrataFrame Forum

Custom Property Not Updating

http://forum.strataframe.net/Topic19968.aspx

By Bill Cunnien - 10/8/2008

I have seven integer fields from my table represented in a business object.  One custom property adds the values and returns the total (int).  A second custom property determines the percentage (total points / total available) -- called rating.  A third custom property references the percentage and presents a letter grade (A, B, C, or D).  Rather simple.  If I click new on the form, the defaults work fine (all int fields maxed at 10, total points is 70, the rating is 100% and the grade is 'A').  The moment I change a value in any one of the seven integer fields, the total points and the rating are zeroed out.  The grade always reverts to the lowest possible choice.  What are the possible reasons that my custom fields are not updating properly?


[Browsable(false),
BusinessFieldDisplayInEditor(),
Description("Total Points"),
DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public int TotalPoints
{
   
get
   
{
       
int mTotalPoints = ontimedelivery + complaints + returns + scrap + profitability + convenience + growthpotential;
       
return mTotalPoints;
    }
}

[
Browsable(false),
BusinessFieldDisplayInEditor(),
Description("Rating"),
DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public double Rating
{
   
get
   
{
       
double mRating = (double)(TotalPoints / 70);
       
return mRating;
    }
}

[
Browsable(false),
BusinessFieldDisplayInEditor(),
Description("Grade"),
DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public String Grade
{
   
get
   
{
       
if (Rating >= 0.91) { return "A"; }
       
if (Rating >= 0.81) { return "B"; }
       
if (Rating >= 0.71) { return "C"; }
       
return "D";
    }
}

Thanks!
Bill

By Edhy Rijo - 10/8/2008

Hi Bill,

I would put a breakpoint at the TotalPoints to see what are the values of the fields you are adding, it looks like something is getting ugly there and is returning less than 0.71.

By Bill Cunnien - 10/8/2008

Yes...the Rating is coming up as zero for anything less than 70 (70/70 = 1, that looks right).  If I modify one of the integer values, the debugger clearly shows the TotalPoints at 68, but 68/70 = 0, for some reason.  Do I have a type problem here somewhere?  Am I misusing the custom properties in some way?

This is quite odd.
Bill

By Bill Cunnien - 10/8/2008

This works:


public double Rating
{
   
get
   
{
       
double mLimit = 70;
       
double mRating = TotalPoints / mLimit;
       
return mRating;
    }
}

Why?

(scratches head)
(contemplates retirement)
(waits eagerly for "yabba-dabba-doo time")

Bill

By Edhy Rijo - 10/8/2008

It looks like a type problem, I use VB instead of C#, but I guess this would also work.


public double Rating
{
   
get
   
{
        mRating = TotalPoints / 70;
       
return (double) mRating;
    }
}
By Greg McGuffey - 10/8/2008

Nope, it won't. In VB, the concept works (dividing two integers, casting to double..get correct answer). In C#, it does integer division, then the case...hence getting zero. In C# you have to cast all the numbers to double before you do the calculation:



// This returns zero

double mRating = (double)(68 / 70);



// This returns correct answer (.97...)

double mRating = (double)68 / (double)70;




A great tool to investigate this sort of thing is Snippet Compiler (http://www.sliver.com/dotnet/SnippetCompiler/). It allows you to try out little snippets of code (instead of having to start another VS instance and creating a project...yada yada).