Mystery of the Setter

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Hello,
I have a question. Itâll take some time to frame it but Iâll try to be as concisely explicit as possible.
I was messing around trying to create a program that would read fields from a bunch of files and create a mean and standard deviation for each. My approach was to use a List<AClass> with a constructor that took the filename as an argument with
FileName being a property. Instead of creating a constructor with a lot of arguments, I used setters to add the values to each instance of the class I retrieved from its respective file. Hereâs the main instruction to add the files:

<div style="color:black; background-color:white
<pre>list.Add(<span style="color:blue new AClass(<span style="color:#a31515 @"Test1.txt"));
[/code]


I used a method to read data from each comma separated file splitting on the commas. The getData method looked like this (Some stuff is left out for brevity):

<div style="color:black; background-color:white
<pre>reader = <span style="color:blue new StreamReader(list.FileName);

<span style="color:blue string[] fields;

<span style="color:blue for (<span style="color:blue int i = 0; i < list.Count; i++)
{
<span style="color:blue while (reader.Peek() != -1)
{

fields = reader.ReadLine().Trim().Split(<span style="color:#a31515 ,); <span style="color:green //split record on ,
ac.setFloatThing2(<span style="color:blue float.Parse(fields[1]));
.
.
.
.
}<span style="color:green //end while
}<span style="color:green //end for

reader.Close();

[/code]

All non setter/getter methods are static in main.
To get an average for each field I created an âAvgâ Property for each and modified the setter of each field like this:


<div style="color:black; background-color:white
<pre><span style="color:green //NUM_FILES = equals the number of files = list.Count

<span style="color:blue public <span style="color:blue void setFloatThing2(<span style="color:blue float ft2)
{
<span style="color:blue this.floatThing2 = ft2;
AvgFloatThing2 += floatThing2 / (<span style="color:blue float)NUM_FILES;
}


[/code]

The getter looks like this:

<div style="color:black; background-color:white
<pre><span style="color:blue public <span style="color:blue float getFloatThing2()
{
<span style="color:blue return floatThing2;
}


[/code]

Next, to get the standard deviation of each field, I wrote a method with some of the code below using the standard deviation formula:

<div style="color:black; background-color:white
<pre><span style="color:green //I didnât realize I needed doubles to work with System.Math so everything is cast as floats.
<span style="color:green //ac is a reference to AClass
<span style="color:green //SDFloatThing2 is a property
<span style="color:green //list refers to List<AClass>

ac.SDFloatThing2 =

(<span style="color:blue float)Math.Sqrt((Math.Pow((list[0].getFloatThing2() -

ac.AvgFloatThing2), 2)
+ Math.Pow((list[1].getFloatThing2() -

ac.AvgFloatThing2), 2)
+ Math.Pow((list[2].getFloatThing2() -

ac.AvgFloatThing2), 2)) / (<span style="color:blue float)list.Count);


[/code]

<br/>
Hereâs where the problems began. The averages returned with the proper values but the getters always returned zero. So I decided to modify getData() as such:
<div style="color:black; background-color:white
<pre><span style="color:blue for (<span style="color:blue int i = 0; i < list.Count; i++)
{
<span style="color:blue while (reader.Peek() != -1)
{
fields = reader.ReadLine().Trim().Split(<span style="color:#a31515 ,); <span style="color:green //split record on ,
list.setFloatThing2(<span style="color:blue float.Parse(fields[1]));
}<span style="color:green //end while
}<span style="color:green //end for
reader.Close();

[/code]

<br/>

Here, the getters returned the right value but now the average was always zero. Stepping through revealed that AvgFloatThing2 in the setter was always âresettingâ. The only way SDFloatThing2 gave the correct result was when I took the
instruction AvgFloatThing2 += floatThing2 / (float)NUM_FILES out of the setter and placed it directly after list.setFloatThing2(float.Parse(fields[1])) in the getData method.
So it works. I just donât know why. Understanding why or why not would go along way in my overall understanding of the Object Oriented stuff. Nothing critical or school related but anyone wanting to wade in with info is appreciated.
AvgCodeGuy
<br/>

View the full article
 
Back
Top