Reputation:
What I basically have is a file that formatted with Name|Value.
There is several different values through the whole file and for each value that matches my if statement I simply want to do total - value and then return the total to do the next calculation, so basically I have a file that is.
1|100
2|200
A|30
B|40
here is what i tried bit it didn't give me what i was looking for, it kept returing alpha and numb as the original value.
int alpha = 1000;
int numb= 500;
int numbtotal = 0;
string[] valu = File.ReadAllLines(subdirectory + "\\" + "values.txt");
foreach (string val in valu)
{
string[] valusplit = val.Split('|');
if(valusplit[0].Equals("1"))
{
numbtotal = Convert.ToInt32(valusplit[1]);
numb = Math.Abs(numb - numbtotal);
}
else if(valueplit[0].Equals("2"))
{
numbtotal = Convert.ToInt32(valusplit[1]);
numb = Math.Abs(numb - numbtotal);
}
}
This basically kept doing 500 - the new valusplit value. When I really wanted to do (500 - 100 = 400) then (400 - 200 = 200) and get the value of 200.
Upvotes: 1
Views: 146
Reputation: 3197
I think that your problem was "splitfilesys" instead of "valusplit" in the "elseif".
int alpha = 1000;
int numb = 500;
int numbtotal = 0;
//string[] values = File.ReadAllLines(subdirectory + "\\" + "values.txt");
string[] values = new String[] { "1|100", "2|200", "A|30", "B|40" };
foreach (string value in values)
{
string[] valueSplit = value.Split('|');
switch (valueSplit[0])
{
case "1":
numb = Math.Abs(numb - Convert.ToInt32(valueSplit[1]));
break;
case "2":
numb = Math.Abs(numb - Convert.ToInt32(valueSplit[1]));
break;
}
}
Upvotes: 1
Reputation: 4801
In your second if statement, you have misspelled valusplit
as valueplit
. If this code compiles as shown (IE valueplit
is indeed a variable defined outside of this code), that would explain why you are getting the wrong result.
Upvotes: 0