0

I want to get result of a value with if condition.

i have get some value in xml file.

now what I want is

if I have a variable "a" here i have assigned some values by using dataset. and i have another variable "b" is assigned value from xml file.

for example int a=25; string b=">10"

now I want to check the condition if condition with out ">" because the symbol present in b variable. I dont know how to check this condition can anybody explain me how to acheive this.

I tried like this but not working if(a+b)

Vinoth
  • 972
  • 1
  • 16
  • 47

4 Answers4

1

You can have some function to remove non numeric characters:

public int Parse(string x)
{
  x = Regex.Replace(x, "[^0-9.]", "");
  int result = 0;
  int.TryParse(x , out result);
  return result; 
}

If its always a number with a symbol then:

symbol = b[0];
int bval = int.Parse(b.Substring(1))

And considering your comment for comparison you can do:

if((symbol=='>'&&a>b)||
   (symbol=='='&&a==b)||
   (symbol=='<'&&a<b)
  ){
       //do your magic here
   }

Of course you may need only one of < = > or you may need to have separate if conditions for each, what ever suits your needs, but I just wanted to give the idea.

Ashkan Mobayen Khiabani
  • 33,575
  • 33
  • 102
  • 171
  • I want that symbol also @Ashkan – Vinoth Sep 08 '17 at 12:10
  • That's only half of the problem and really the easy part. You also have to inspect `b` for the operator to determine what operation needs to be done to compare `a` to the numeric part of `b`. – juharr Sep 08 '17 at 12:12
  • if that's the case this becomes even easier. You can just use substring to set two separate variables; one for the operator and one for the number, then go on about your day. – MaCron Sep 08 '17 at 12:23
  • i'm getting error in string symbol = b[0]; cannot implicit convert type char to string – – Vinoth Sep 11 '17 at 11:51
  • Use b[0].ToString() to convrt it to string – Ashkan Mobayen Khiabani Sep 11 '17 at 12:12
  • Hi Ashkan Mobayen, here how can i make IF condition if(a+symbol+bval){} if i do this in if condition. i'm getting error. I don't know how to use this in if condition – Vinoth Sep 12 '17 at 06:29
1

You can use the DataTable.Compute-"trick" to evaulate such expressions:

int a = 25;
string b = ">10";
bool isTrue = (bool)new DataTable().Compute($"{a}{b}", null);  // true

What is supported you can read at the DataColumn.Expression remarks.


if the condition is 1!=10, how to use not equal in this code .this condition is not working what should i do.

As the documentation tells you that is not valid syntax, you have to use <> (look at operators). So a simple approach would be either to use <> in the first place or replace them:

b = b.Replace("!=", "<>");
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
0

First separate symbol from b:

string symbol = b[0].ToString();
string numberString = b.SubString(1);
int number = int.Parse(numberString);

Now use switch to get operation for symbol and compare:

bool result = false;
switch (symbol)
{
    case ">":
        if (a > number)
        {
            result = true;
        }
        break;
}

EDIT: Changed symbol declaration to avoid error: "cannot implicit convert type char to string"

koca2000
  • 67
  • 12
0

I tried like this

if (b.Contains(">")) {
                                b = b.Replace(">", "");
                                if (a >Convert.ToInt32(b))
                                {
                                    Console.WriteLine("value is less");
                                }
                                else
                                {
                                    Console.WriteLine("value is Greater");
                                }
                            }

similarly all the symbols

Vinoth
  • 972
  • 1
  • 16
  • 47
  • You need to do something like this var operator = b.substring(0); int number = Convert.ToInt32(b.remove(0,1)); if(operator == ">") {if a>number){do stuff}else{do otherstruff} – MaCron Sep 08 '17 at 12:51