Code is unreachable [closed] - c#

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
Relatively new to programming but I can't work out why my If statement is unreachable. Also what would be the best way to return the calculation so I can send it back?
while (Reader.Read()) {
if (Reader.NodeType == XmlNodeType.Element)
{
if (Reader.Name == "Amount")
{
if ("Amount" == "Gold" || value > 4999)
{
credit = 300;
DiscountPercent = .20f;
}
else if ("Amount" == "Silver" || value > 4999)
{
DiscountPercent = .15f;
}
else if ("Amount" == "Regular")
{
credit = 200;
}
}
else if (Reader.Name == "Member")
{
if ("Member" == "Gold" || value > 4999)
{
credit = 300;
DiscountPercent = .20f;
}
else if ("Member" == "Silver" || value > 4999)
{
DiscountPercent = .15f;
}
else if ("Member" == "Regular")
{
credit = 200;
}
}
}
}
}
Could it be because I'm not including a break statement?

"Amount" == "Gold" will never evaluate to true, because the strings are different. Same goes for other string comparisons. Compiler reduces this false out of the ||, so it "sees" the following:
if (value > 4999) {
credit = 300;
DiscountPercent = .20f;
} else if (value > 4999) {
DiscountPercent = .15f;
} else if (false) {
credit = 200;
}
The compiler reasonably concludes that the middle and the bottom ifs are unreachable.
To fix this code, read the value, and use it in your comparisons:
if (Reader.Name == "Amount") {
var amount = Reader.Value;
if (amount == "Gold" || value > 4999) {
credit = 300;
DiscountPercent = .20f;
} else if (amount == "Silver" || value > 4999) {
DiscountPercent = .15f;
} else if (amount == "Regular") {
credit = 200;
}
}

I've re-written your code to make it compile:
static void Main(string[] args)
{
XmlReaderSettings settings = new XmlReaderSettings();
settings.DtdProcessing = DtdProcessing.Parse;
XmlReader Reader = XmlReader.Create("items.xml", settings);
int value = 500;
int credit = 0;
var DiscountPercent = 0.1f;
while (Reader.Read())
{
if (Reader.NodeType == XmlNodeType.Element)
{
if (Reader.Name == "Amount")
{
if ("Amount" == "Gold" || value > 4999)
{
credit = 300;
DiscountPercent = .20f;
}
else if ("Amount" == "Silver" || value > 4999)
{
DiscountPercent = .15f;
}
else if ("Amount" == "Regular")
{
credit = 200; //unreachable code detected
}
Reader.Read();
}
else if (Reader.Name == "Member")
{
if ("Member" == "Gold" || value > 4999)
{
credit = 300;
DiscountPercent = .20f;
}
else if ("Member" == "Silver" || value > 4999)
{
DiscountPercent = .15f;
}
else if ("Member" == "Regular")
{
credit = 200; //unreachable code detected
}
Reader.Read();
}
}
}
}
I've added some suggestions to help you figure out what's going on...
static void Main(string[] args)
{
XmlReaderSettings settings = new XmlReaderSettings();
settings.DtdProcessing = DtdProcessing.Parse;
XmlReader reader = XmlReader.Create("items.xml", settings);
int value = 500;
int credit = 0;
var discountPercent = 0.1f; //generally people use lower case for variables
var amountType = "Gold"; //added this variable to replace constant
var memberType = "Gold"; //added this variable to replace constant
while (reader.Read()) //generally people use lower case for variables "reader.Read()"
{
if (reader.NodeType == XmlNodeType.Element)
{
if (reader.Name == "Amount")
{
//two constant but different strings will never equal each other...
//, just like the integer 1 will never equal 2
if ("Amount" == amountType || value > 4999)
{
credit = 300;
discountPercent = .20f;
}
else if ("Amount" == amountType || value > 4999)
{
discountPercent = .15f;
}
else if ("Amount" == amountType)
{
credit = 200; //no error, code is now reachable
}
reader.Read();
}
else if (reader.Name == "Member")
{
if ("Member" == memberType || value > 4999)
{
credit = 300;
discountPercent = .20f;
}
else if ("Member" == memberType || value > 4999)
{
discountPercent = .15f;
}
else if ("Member" == memberType)
{
credit = 200; //no error, code is now reachable
}
reader.Read();
}
}
}
}

Related

Unity Color Matching Game

I have searched the similar games on forum and google but i could not find exactly.
I am making a puzzle game. and user can get point if the nodes (horizontal sticks) are same color then he can get.
when they are in same direction it says colors matched but in generated node whenever i rotate the sticks it says also same.
Can you take a look? and tell me how to fix. Also if you have better idea about this matching I will be appreciated.
---------
void Update()
{
if (Input.GetMouseButtonDown(0))
{
clickTime = Time.time;
rayhit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero, Mathf.Infinity, selectableObjLayerMask);
}
else if (Input.GetMouseButtonUp(0))
{
if (rayhit)
{
if (Time.time - clickTime < .2f)
{
Node node = rayhit.transform.GetComponent<Node>();
if (node != null)
{
for (int i = 0; i < node.sticks.Count; i++)
{
Vector3 newAngles = new Vector3(0, 0, (node.sticks[i].transform.localEulerAngles.z - 45));
newAngles.z = newAngles.z < 0 ? newAngles.z + 180 : newAngles.z;
newAngles.z = newAngles.z >180 ? newAngles.z - 180 : newAngles.z;
node.sticks[i].transform.localEulerAngles = newAngles;
node.sticks[i].degree = (int)newAngles.z;
//******** HERE IS COLOR MATCHING*******
if (node.transform.parent.name=="Node1" && node.sticks[i].degree == 90)
{
colorMatch[1] = node.sticks[i].color;
Debug.Log("COLOR 1___"+ colorMatch[1]);
//Debug.Log(colorMatch1);
}
if (node.transform.parent.name == "Node2" && node.sticks[i].degree == 90)
{
colorMatch[2] = node.sticks[i].color;
Debug.Log("COLOR 2___" + colorMatch[2]);
}
if (node.transform.parent.name == "Node3" && node.sticks[i].degree == 90)
{
colorMatch[3] = node.sticks[i].color;
Debug.Log("COLOR 3___" + colorMatch[3]);
//if (colorMatch[1] == colorMatch[2] && colorMatch[2] == colorMatch[3])
//{
// Debug.Log("COLORS MATCHED : " + colorMatch[1]);
//}
}
if (colorMatch[1]==colorMatch[2] && colorMatch[2]==colorMatch[3])
{
Debug.Log("COLOR MATCHED");
}
}
}
}
else
{
Node currNode = rayhit.transform.GetComponent<Node>();
if(currNode.isMoved == false)
{
smallestId = 0;
smallestDistance = 999;
for (int i = 0; i < nodes.Length; i++)
{
float distance = Vector2.Distance(rayhit.transform.position, nodes[i].transform.position);
if (smallestDistance > distance)
{
smallestDistance = distance;
smallestId = i;
}
}
rayhit.transform.position = nodes[smallestId].transform.position;
if (rayhit.transform.parent != nodes[smallestId].transform)
{
if (nodes[smallestId].transform.childCount > 0 && nodes[smallestId].transform != rayhit.transform.parent)
{
if (currNode != null)
{
for (int i = 0; i < currNode.sticks.Count; i++)
{
nodes[smallestId].transform.GetChild(0).GetComponent<Node>().sticks.Add(currNode.sticks[i]);
currNode.sticks[i].transform.SetParent(nodes[smallestId].transform.GetChild(0));
}
Destroy(rayhit.transform.gameObject);
}
}
else
{
if (currNode != null)
{
currNode.isMoved = true;
}
rayhit.transform.SetParent(nodes[smallestId].transform);
}
}
}
}
}
rayhit = new RaycastHit2D();
}
else if (Input.GetMouseButton(0))
{
if(rayhit.transform != null)
{
Node currNode = rayhit.transform.GetComponent<Node>();
if(currNode != null)
if (currNode.isMoved == false)
{
if (Time.time - clickTime >= 0.2f)
{
Vector2 newPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
rayhit.transform.position = newPos;
}
}
}
}
}
if (node.transform.parent.name=="Node1" && node.sticks[i].degree == 90)
{
colorMatch[1] = node.sticks[i].color;
Debug.Log("COLOR 1___"+ colorMatch[1]);
//Debug.Log(colorMatch1);
}
if (node.transform.parent.name == "Node2" && node.sticks[i].degree == 90)
{
colorMatch[2] = node.sticks[i].color;
Debug.Log("COLOR 2___" + colorMatch[2]);
}
if (node.transform.parent.name == "Node3" && node.sticks[i].degree == 90)
{
colorMatch[3] = node.sticks[i].color;
Debug.Log("COLOR 3___" + colorMatch[3]);
if (colorMatch[1] == colorMatch[2] && colorMatch[2] == colorMatch[3])
{
Debug.Log("COLORS MATCHED : " + colorMatch[1]);
}
}
Here is working code. but how i can destroy the matched sticks?

How to make my code evaluate the whole arithmetic expression?

I am trying to make a string calculator, it works fine with two numbers but I always encounter a problem when evaluating multiple operations:
7*2+4=
Also can you help me with my multiplication and division code. I don't understand why it prints 0 even with just two numbers(7*5)
using System;
using System.Text.RegularExpressions;
namespace Sariling_Calcu
{
class Program
{
private static string exp;
private static int[] i = new int[1000];
private static char[] oper = new char[10];
private static int cntr2;
private static int result;
private static int pluscount;
private static int subcount;
private static int mulcount;
private static int divcount;
static void getNum()
{
string[] strNum = (Regex.Split(exp, #"\D+"));
for (int cntr = 0; cntr < exp.Length; cntr++)
{
foreach (string item in strNum)
{
if (!string.IsNullOrEmpty(item))
{
i[cntr] = int.Parse(item);
cntr += 1;
}
}
}
}
static void counter()
{
for (int cntr = 0; cntr < exp.Length; cntr++)
{
if (exp[cntr] == '+')
{
pluscount++;
}
else if (exp[cntr] == '-')
{
subcount++;
}
else if (exp[cntr] == '*')
{
mulcount++;
}
else if (exp[cntr] == '/')
{
divcount--;
}
}
}
static void oprtr()
{
for (int cntr = 0; cntr < exp.Length; cntr++)
{
if (exp[cntr] != '1'
&& exp[cntr] != '2'
&& exp[cntr] != '3'
&& exp[cntr] != '4'
&& exp[cntr] != '5'
&& exp[cntr] != '6'
&& exp[cntr] != '7'
&& exp[cntr] != '8'
&& exp[cntr] != '9')
{
if (exp[cntr] == '+')
{
result += i[cntr2];
cntr2 += 1;
pluscount--;
if (pluscount == 0 && subcount == 0 && mulcount==0 && divcount==0)
{
cntr2 += 3;
result += i[cntr2];
}
}
else if (exp[cntr] == '-')
{
result -= i[cntr2];
cntr2 += 1;
subcount--;
result = -result;
if (pluscount == 0 && subcount == 0 && mulcount == 0 && divcount == 0)
{
cntr2 += 3;
result -= i[cntr2];
}
}
else if (exp[cntr] == '*')
{
if (result == 0)
{
result += 1;
}
result *= i[cntr2];
cntr2 += 1;
mulcount--;
if (pluscount == 0 && subcount == 0 && mulcount == 0 && divcount == 0)
{
cntr2 += 3;
result *= i[cntr2];
}
}
else if (exp[cntr] == '/')
{
if (result == 0)
{
result += 1;
}
result /= i[cntr2];
cntr2 += 1;
divcount--;
if (pluscount == 0 && subcount == 0 && mulcount == 0 && divcount == 0)
{
cntr2 += 3;
result /= i[cntr2];
}
}
}
}
}
static void Main(string[] args)
{
Console.Write("Expression: ");
exp = Console.ReadLine();
counter();
getNum();
oprtr();
Console.Write("Answer: \n" + result);
Console.ReadLine();
}
}
}
you could use LINQ to reduce your code to a few lines but looks like its a school assignment where you would have to go with Arrays and loops.
I have tried to refine your code a bit and did a few fixes, change getNum(), counter() and oprtr() methods as below and let me know if it works, then I would add some comments in the code to explain changes I made.
static void getNum()
{
string[] strNum = (Regex.Split(exp, #"\D+"));
for (int cntr = 0; cntr < strNum.Length; cntr++)
{
if (!string.IsNullOrEmpty(strNum[cntr]))
{
i[cntr] = int.Parse(strNum[cntr]);
}
}
}
static void counter()
{
cntr2 = 0;
for (int cntr = 0; cntr < exp.Length; cntr++)
{
if (exp[cntr] == '+')
{
oper[cntr2] = '+';
cntr2++;
}
else if (exp[cntr] == '-')
{
oper[cntr2] = '-';
cntr2++;
}
else if (exp[cntr] == '*')
{
oper[cntr2] = '*';
cntr2++;
}
else if (exp[cntr] == '/')
{
oper[cntr2] = '/';
cntr2++;
}
}
}
static void oprtr()
{
result = i[0];
cntr2 = 1;
for (int cntr = 0; cntr < oper.Length; cntr++)
{
if (oper[cntr] == '+')
{
result += i[cntr2];
}
else if (oper[cntr] == '-')
{
result -= i[cntr2];
}
else if (oper[cntr] == '*')
{
result *= i[cntr2];
}
else if (oper[cntr] == '/')
{
if (i[cntr2] == 0)
{
throw new DivideByZeroException();
}
result /= i[cntr2];
}
cntr2 += 1;
}
}

Text box stops updating after a condition occurs

In this program, whenever any of the statements under else occur, it doesn't go back to state 0 and perform the actions in that state, which is what I'm trying to do. Instead, the txtStatus, and txtScore boxes just continue to display "Roll again!" which is what is displayed when jumping to state 2. What am I doing wrong here?
int die1 = 0;
int die2 = 0;
int total = 0;
int state = 0;
int point = 0;
//int point2;
int score = 0;
//private int score;
//private int state;
public Form1()
{
InitializeComponent();
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
}
private void label2_Click(object sender, EventArgs e)
{
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void pictureBox2_Click(object sender, EventArgs e)
{
}
private void btnRoll_Click(object sender, EventArgs e)
{
picDie1L.Visible = false;
picDie1R.Visible = false;
picDie2L.Visible = false;
picDie2R.Visible = false;
picDie3L.Visible = false;
picDie3R.Visible = false;
picDie4L.Visible = false;
picDie4R.Visible = false;
picDie5L.Visible = false;
picDie5R.Visible = false;
picDie6L.Visible = false;
picDie6R.Visible = false;
Random rand = new Random();
die1 = rand.Next(1, 7);
die2 = rand.Next(1, 7);
total = (die1 + die2);
txtDie1.Text = die1.ToString();
txtDie2.Text = die2.ToString();
txtTotal.Text = total.ToString();
{
if (die1 == 1)
{
picDie1L.Visible = true;
}
if (die1 == 2)
{
picDie2L.Visible = true;
}
if (die1 == 3)
{
picDie3L.Visible = true;
}
if (die1 == 4)
{
picDie4L.Visible = true;
}
if (die1 == 5)
{
picDie5L.Visible = true;
}
if (die1 == 6)
{
picDie6L.Visible = true;
}
if (die2 == 1)
{
picDie1R.Visible = true;
}
if (die2 == 2)
{
picDie2R.Visible = true;
}
if (die2 == 3)
{
picDie3R.Visible = true;
}
if (die2 == 4)
{
picDie4R.Visible = true;
}
if (die2 == 5)
{
picDie5R.Visible = true;
}
if (die2 == 6)
{
picDie6R.Visible = true;
}
if (state == 0)
{
picPuck4.Visible = false;
picPuck5.Visible = false;
picPuck6.Visible = false;
picPuck8.Visible = false;
picPuck9.Visible = false;
picPuck10.Visible = false;
txtStatus.Text = string.Empty;
state = 1;
txtPoint.Text = string.Empty;
}
}
if (state == 1)
{
if (total == 7 || total == 11)
{
txtStatus.Text = "You are a winner!";
score++;
txtScore.Text = Convert.ToString(score);
state = 0;
}
if (total == 2 || total == 3 || total == 12)
{
txtStatus.Text = "You lose. Play again!";
score--;
txtScore.Text = Convert.ToString(score);
state = 0;
}
if (total == 4 || total == 5 || total == 6 || total == 8 || total == 9 || total == 10 || total == 12)
{
txtStatus.Text = "Roll again!";
point = int.Parse(txtTotal.Text);
txtPoint.Text = point.ToString();
state = 2;
if (total == 4)
picPuck4.Visible = true;
if (total == 5)
picPuck5.Visible = true;
if (total == 6)
picPuck6.Visible = true;
if (total == 8)
picPuck8.Visible = true;
if (total == 9)
picPuck9.Visible = true;
if (total == 10)
picPuck10.Visible = true;
}
}
else
{
if (point == total)
{
txtStatus.Text = "You are a winner!";
score++;
txtScore.Text = Convert.ToString(score);
state = 0;
}
if (total == 7)
{
txtStatus.Text = "You lose. Play again!";
score--;
txtScore.Text = Convert.ToString(score);
state = 0;
}
if (total != 7 || point != total)
{
txtStatus.Text = "Roll again!";
state = 2;
}
}
}
}
}
I think you should change the last else statement to this:
if (point == total)
{
txtStatus.Text = "You are a winner!";
score++;
txtScore.Text = Convert.ToString(score);
state = 0;
}
else if (total == 7)
{
txtStatus.Text = "You lose. Play again!";
score--;
txtScore.Text = Convert.ToString(score);
state = 0;
}
else
{
txtStatus.Text = "Roll again!";
state = 2;
}
Explanation: When total has any value but 7 you switch state to 2. Because of that, when you roll again you immediately go the last else statement and execute those 3 if statements inside it. In this scenario, the last if statement is always true (since total is never 7 in this scenario) therefore the text remains "Roll again" and state is never changed (remains 2).
This will fix your issue, but I think you need to change the logic (for example, point is always equal to total, since point == txtTotal.text == total which may not be desired behavior).
Hope this helps.
maybe the problem is here
if (total != 7 || point != total)
{
txtStatus.Text = "Roll again!";
state = 2;
}
as you see the condition is most of the time true! the only time that this is false is when total == point == 7

Compare params array of objects

I'm working on a method that compares a collection of objects (since the object supports all types), if they are all the same, it returns true, but if one or more differs, it returns false.
This is what I have:
public static bool Compare(bool compareTypes = false, params object[] values)
{
if (!compareTypes)
{
if (values is null || values.Length == 0) return false;
else if (values.Length == 1) return true;
else return values[0] == values[1];
}
else
{
if (values is null || values.Length == 0) return false;
else if (values.Length == 1) return true;
else return values[0].GetType() == values[1].GetType();
}
}
And it works fine as long as the number of objects is 2.
My problem is that I want the method to compare the value or the type of all parameters that are passed regardless of the amount
This is what I want my method to do
Compare (false, "Hello", "Hello") //True
Compare(false, "Hello", "Bye") //False
Compare(true, 0, 1) //True
Compare(true, "Hi", 20) //False
Compare(false, "LOL", "LOL", "LOL", "LOL") //True
Compare(false, "LOL", "LOL", "LOL", "lol") //False
If you are ok with the logic that works for 2 objects why not just compare all objects in a same way with a loop?
public static bool Compare(bool compareTypes = false, params object[] values)
{
if (!compareTypes)
{
if (values == null || values.Length == 0)
{
return false;
}
else if (values.Length == 1)
{
return true;
}
else if (values.Length == 2){
return values[0] == values[1];
}
else
{
bool result = true;
for(int i = 0; i < values.Length - 1; i++)
{
if(values[i] != values[i+1])
{
result = false;
break;
}
}
return result;
}
}
else
{
if (values == null || values.Length == 0)
{
return false;
}
else if (values.Length == 1)
{
return true;
}
else if (values.Length == 2)
{
return values[0].GetType() == values[1].GetType();
}
else
{
bool result = true;
for(int i = 0; i < values.Length - 1; i++)
{
if(values[i].GetType() != values[i+1].GetType())
{
result = false;
break;
}
}
return result;
}
}
}
Here is a running example with your test cases : https://dotnetfiddle.net/uDA5Mb
You can also use recursive and simplify your code with that way.
public static bool Compare(bool compareTypes = false, params object[] values)
{
if (values is null || values.Length == 0)
return false;
else if (values.Length == 1)
return true;
else if (values.Lenght == 2)
if (!compareTypes)
return values[0] == values[1];
else
return values[0].GetType() == values[1].GetType();
else
{
for(int i = 0; i < values.Lenght - 1; i++)
if(!Compare(compareTypes, values[i], values[i+1]))
return false;
return true;
}
}
Very simple.

How to calculate a formula and add a row in data table

I have a data from database in a DaTaTable Object and i am performing a row calculation based on some formula defined in a list of string but unfortunately i am ending up with wrong calculation because Sequence of rows value does not match with given formula so calculations going wrong
For e.x ( Income before pipeline, depr/amort and MSR )= Net Production Income - Total Personal Cost - Other Cost + Service Fee Income
For that in a loop first value is coming from datatable is Total Personal Cost and second is Other Cost third is Service Fee Income and in last Net Production Income. which form this formula
Income before pipeline, depr/amort and MSR = Total Personal Cost - Other Cost + Service Fee Income + Net Production income.
Please give me a solution and in that particular below code this code is performing row calculation for given formula
for (int P = 0; P < dt.Rows.Count; P++)
{
rowName = dt.Rows[P]["GLDescription"].ToString();
AddColume[0] = "";
if (AddColume.Contains(rowName))
{
int l = 1;
var DRValue = dt.Rows[P][j];
if (!(DRValue == null || DRValue == DBNull.Value))
{
if (Expression[index] == "+")
{
totalRow += Convert.ToDecimal(DRValue);
}
else if (Expression[index] == "-")
{
totalRow -= Convert.ToDecimal(DRValue);
}
index++;
}
}
else if (AddColume.Contains(rowName) && AddColume.Contains("self"))
{
var DRValue = dt.Rows[P][j];
if (!(DRValue == null || DRValue == DBNull.Value))
{
decimal CurrentValue = Convert.ToDecimal(DRValue);
dt.Rows[P][j] = CurrentValue * -1;
}
}
}
for you support i have this code
List<string> lstGrouping = new List<string>();
lstGrouping.Add("Volume↔Volume↔Volume↔Sigular");
lstGrouping.Add("Units↔Units↔Units↔Sigular");
lstGrouping.Add("Blank↔Blank↔Sigular");
lstGrouping.Add("Total Production Revenue↔Secondary-Net (Sell - Buy +/- Hedge)↔Branch Brokered Loan Income↔add");
lstGrouping.Add("Total Direct Costs↔Deed Recording↔Provisions for Loan Losses↔add");
lstGrouping.Add("Net Production Income↔Total Production Revenue↔Total Direct Costs↔+$-");
lstGrouping.Add("Total Personnel Costs↔Accounting Dept↔Employer payroll tax exp↔add");
lstGrouping.Add("Total G&A Costs and Other↔Bank Fees↔Provisions for Branch Losses↔add");
lstGrouping.Add("Servicing Fee Income↔Servicing Fee Income , net of expenses↔Servicing Fee Income , net of expenses↔Sigular");
lstGrouping.Add("Income before pipeline, depr/amort and MSR↔Net Production Income↔Total Personnel Costs↔Total G&A Costs and Other↔Servicing Fee Income↔+$+$-$-");
lstGrouping.Add("Other items Total↔Servicing Fee Income , net of expenses↔Depreciation Expense↔add");
lstGrouping.Add("Net Income↔Income before pipeline, depr/amort and MSR↔Other items Total↔+$+");
for (int i = 0; i < lstGrouping.Count; i++)
{
var AddColume = lstGrouping[i].Split('↔');
var Expression = AddColume[(AddColume.Length) - 1].Split('$');
int startindex = 0;
int Endindex = 0;
foreach (DataRow DR in dt.Rows)
{
string GlDescription = DR["GLDescription"].ToString();
if (GlDescription.Trim() == AddColume[1].Trim())
startindex = dt.Rows.IndexOf(DR);
else if (GlDescription.Trim() == AddColume[2].Trim())
Endindex = dt.Rows.IndexOf(DR);
}
DataRow NewDR = dt.NewRow();
if (Expression[0] == "add")
dt.Rows.InsertAt(NewDR, Endindex + 1);
else if (Expression[0] == "Sigular")
{
Endindex = 1;
}
else
dt.Rows.Add(NewDR);
DataRow level1Row = dtLevel1.NewRow();
dtLevel1.Rows.Add(level1Row);
if (startindex != null && Endindex != null)
{
for (int j = 0; j < dt.Columns.Count; j++)
{
string ColName = dt.Columns[j].ColumnName;
if (ColName != "GLDescription" && ColName != "GLDescriptionId")
{
decimal totalRow = 0;
if (Expression[0] == "add")
{
for (int K = startindex; K <= Endindex; K++)
{
var DRValue = dt.Rows[K][j];
if (DRValue == null || DRValue == DBNull.Value)
DRValue = 0;
if (Expression[0].Contains("add"))
totalRow += Convert.ToDecimal(DRValue);
}
}
else if (Expression[0] == "Sigular")
{
int KeyValue = startindex;
var DRValue = dt.Rows[KeyValue][j];
if (DRValue == null || DRValue == DBNull.Value)
DRValue = 0;
totalRow += Convert.ToDecimal(DRValue);
}
else
{
int index = 0;
int paraindex = 0;
string rowName;
for (int P = 0; P < dt.Rows.Count; P++)
{
rowName = dt.Rows[P]["GLDescription"].ToString();
AddColume[0] = "";
if (AddColume.Contains(rowName))
{
int l = 1;
var DRValue = dt.Rows[P][j];
if (!(DRValue == null || DRValue == DBNull.Value))
{
if (Expression[index] == "+")
{
totalRow += Convert.ToDecimal(DRValue);
}
else if (Expression[index] == "-")
{
totalRow -= Convert.ToDecimal(DRValue);
}
index++;
}
}
else if (AddColume.Contains(rowName) && AddColume.Contains("self"))
{
var DRValue = dt.Rows[P][j];
if (!(DRValue == null || DRValue == DBNull.Value))
{
decimal CurrentValue = Convert.ToDecimal(DRValue);
dt.Rows[P][j] = CurrentValue * -1;
}
}
}
}
NewDR[ColName] = totalRow;
level1Row[ColName] = totalRow;
}
else if (ColName == "GLDescription")
{
NewDR[ColName] = AddColume[0];
level1Row[ColName] = AddColume[0];
}
}
}
}
dtLevel2 = dt;
if (level == "lev1")
return dtLevel1;
else if (level == "lev2")
return dt;
else
return null;

Categories

Resources