Saturday 15 May 2010

c# - Operator '==' cannot be applied to operands of type 'int' and 'string -



c# - Operator '==' cannot be applied to operands of type 'int' and 'string -

i'm in middle of writing programme think of number, , computer guesses it. i'm trying test go along, maintain getting error should'nt be. error topic title. used int.parse convert strings, don't know why i'm getting error. know says '==' can't used integers, i've seen online plus slides class utilize it, i'm stuck. code incomplete , i'm not trying run yet, want prepare problem. appreciate help lot, :d

class programme { static void main(string[] args) { console.write("enter number after 5 start: "); int reply = int.parse(console.readline()); { console.writeline("is 3?"); if (answer == "higher")

you inquire number, trying compare non-numeric data. forget language semantics, how expect compare number text? mean inquire if number 3 equal "higher"?

the reply is nonsensical; 1 reason why language not allow it.

int = 1; string b = "hello"; if (a == 1) { /* works; comparing int int */ } if (a == "hello") { /* comparing int string!? not work. */ } if (b == 1) { /* not work -- comparing string int. */ } if (b == "hello") { /* works -- comparing string string. */ }

you can forcefulness comparing compile converting number string:

if (answer.tostring() == "higher")

but status will never met because there no int value convert text "hello". code within of if block guaranteed never execute. might write if (false).

c# string int operator-keyword

No comments:

Post a Comment