Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Operator '==' cannot be applied to operands of type 'string' and 'int'

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

Scheduled Pinned Locked Moved C#
helpcsharpquestion
20 Posts 10 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    bigphish
    wrote on last edited by
    #1

    hi.. am new to c#.. am getting an error below code..wch is wrking fine with Vb..

    int legseq = 0;
    if (legseq == " ")
    {
    legseq = 0;
    }

    Error: Operator '==' cannot be applied to operands of type 'string' and 'int'

    I dont want to use 'null'instaed off " ". and dont want to make int as nullable. can anybdy help? Regrds, Reta

    L L A V M 5 Replies Last reply
    0
    • B bigphish

      hi.. am new to c#.. am getting an error below code..wch is wrking fine with Vb..

      int legseq = 0;
      if (legseq == " ")
      {
      legseq = 0;
      }

      Error: Operator '==' cannot be applied to operands of type 'string' and 'int'

      I dont want to use 'null'instaed off " ". and dont want to make int as nullable. can anybdy help? Regrds, Reta

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      In C#, as in many other languages, " " is a string holding a single space. You can't store text in an integer, all it can hold is a number in a particular range (and 0 is always a good number).

      bigphish wrote:

      I dont want to use 'null'instaed off " ". and dont want to make int as nullable. can anybdy help?

      Maybe you should start by telling what you do want. Or just make it happen. :)

      Luc Pattyn [My Articles] Nil Volentibus Arduum

      B 1 Reply Last reply
      0
      • L Luc Pattyn

        In C#, as in many other languages, " " is a string holding a single space. You can't store text in an integer, all it can hold is a number in a particular range (and 0 is always a good number).

        bigphish wrote:

        I dont want to use 'null'instaed off " ". and dont want to make int as nullable. can anybdy help?

        Maybe you should start by telling what you do want. Or just make it happen. :)

        Luc Pattyn [My Articles] Nil Volentibus Arduum

        B Offline
        B Offline
        bigphish
        wrote on last edited by
        #3

        Thnks for the reply Luc. will the below code can give the same result?

        int? legseq = 0;
        if (legseq == null)
        {
        legseq = 0;
        }

        L P 2 Replies Last reply
        0
        • B bigphish

          hi.. am new to c#.. am getting an error below code..wch is wrking fine with Vb..

          int legseq = 0;
          if (legseq == " ")
          {
          legseq = 0;
          }

          Error: Operator '==' cannot be applied to operands of type 'string' and 'int'

          I dont want to use 'null'instaed off " ". and dont want to make int as nullable. can anybdy help? Regrds, Reta

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          Using null wouldn't turn it into meaningful code either, but at least it would compile. With the warning "The result of the expression is always 'false' since a value of type 'int' is never equal to 'null' of type 'int?'"

          L 1 Reply Last reply
          0
          • B bigphish

            Thnks for the reply Luc. will the below code can give the same result?

            int? legseq = 0;
            if (legseq == null)
            {
            legseq = 0;
            }

            L Offline
            L Offline
            Luc Pattyn
            wrote on last edited by
            #5

            that would compile, however it too does not make sense to me. the variable starts by holding zero, so why compare it to " " in the original code (something it can't hold), or to null here (something it could but won't hold). start by telling what you do want (in functional terms, not in bad code), before you attempt to code it. BTW: the best way (quality wise and speed wise) to learn a new programming language (or programming in general), is by choosing, buying, and studying a book on the subject. And I do mean a real book, not an e-book or a web course. :)

            Luc Pattyn [My Articles] Nil Volentibus Arduum

            1 Reply Last reply
            0
            • B bigphish

              Thnks for the reply Luc. will the below code can give the same result?

              int? legseq = 0;
              if (legseq == null)
              {
              legseq = 0;
              }

              P Offline
              P Offline
              Pete OHanlon
              wrote on last edited by
              #6

              You have introduced a redundant piece of code there. If you leave the type is int rather than nullable int, unless you explicitly assign it a value, it will already be zero. The default value for int is 0.

              Forgive your enemies - it messes with their heads

              "Mind bleach! Send me mind bleach!" - Nagy Vilmos

              My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility

              1 Reply Last reply
              0
              • L Lost User

                Using null wouldn't turn it into meaningful code either, but at least it would compile. With the warning "The result of the expression is always 'false' since a value of type 'int' is never equal to 'null' of type 'int?'"

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #7

                int? test = new int?();
                if (test == null)
                {
                MessageBox.Show("R u sure?");
                }

                Computers have been intelligent for a long time now. It just so happens that the program writers are about as effective as a room full of monkeys trying to crank out a copy of Hamlet.

                L 1 Reply Last reply
                0
                • L Lost User

                  int? test = new int?();
                  if (test == null)
                  {
                  MessageBox.Show("R u sure?");
                  }

                  Computers have been intelligent for a long time now. It just so happens that the program writers are about as effective as a room full of monkeys trying to crank out a copy of Hamlet.

                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #8

                  Yes, I'm sure. You changed the type of test as well as its initialization. Of course it's going to be different.

                  L 1 Reply Last reply
                  0
                  • L Lost User

                    Yes, I'm sure. You changed the type of test as well as its initialization. Of course it's going to be different.

                    L Offline
                    L Offline
                    Lost User
                    wrote on last edited by
                    #9

                    Yeah, I thought you were implying if he used int? (as he said in another post). I got what you meant now... Your wording through me off.

                    Computers have been intelligent for a long time now. It just so happens that the program writers are about as effective as a room full of monkeys trying to crank out a copy of Hamlet.

                    L 1 Reply Last reply
                    0
                    • L Lost User

                      Yeah, I thought you were implying if he used int? (as he said in another post). I got what you meant now... Your wording through me off.

                      Computers have been intelligent for a long time now. It just so happens that the program writers are about as effective as a room full of monkeys trying to crank out a copy of Hamlet.

                      L Offline
                      L Offline
                      Lost User
                      wrote on last edited by
                      #10

                      I guess I could have been clearer.. And I'm still wondering what he actually wants to do.

                      L B 2 Replies Last reply
                      0
                      • L Lost User

                        I guess I could have been clearer.. And I'm still wondering what he actually wants to do.

                        L Offline
                        L Offline
                        Luc Pattyn
                        wrote on last edited by
                        #11

                        He absolutely positively undeniably wants to make sure legseq is zero, I'd say. :)

                        Luc Pattyn [My Articles] Nil Volentibus Arduum

                        1 Reply Last reply
                        0
                        • L Lost User

                          I guess I could have been clearer.. And I'm still wondering what he actually wants to do.

                          B Offline
                          B Offline
                          BillWoodruff
                          wrote on last edited by
                          #12

                          harold aptroot wrote:

                          And I'm still wondering what he actually wants to do.

                          He is creating a security firewall to prevent external hacking that would make his int a string ?

                          "It is the mark of an educated mind to be able to entertain a thought without accepting it." Aristotle

                          1 Reply Last reply
                          0
                          • B bigphish

                            hi.. am new to c#.. am getting an error below code..wch is wrking fine with Vb..

                            int legseq = 0;
                            if (legseq == " ")
                            {
                            legseq = 0;
                            }

                            Error: Operator '==' cannot be applied to operands of type 'string' and 'int'

                            I dont want to use 'null'instaed off " ". and dont want to make int as nullable. can anybdy help? Regrds, Reta

                            A Offline
                            A Offline
                            Abhinav S
                            wrote on last edited by
                            #13

                            What you are doing here is comparing a string " " to an integer. This is not acceptable in C#. You can compare legseq to 0 or convert legseq to string. The default value of an int is 0 so if you dont assign anything to legseq, its value will be 0.

                            1 Reply Last reply
                            0
                            • B bigphish

                              hi.. am new to c#.. am getting an error below code..wch is wrking fine with Vb..

                              int legseq = 0;
                              if (legseq == " ")
                              {
                              legseq = 0;
                              }

                              Error: Operator '==' cannot be applied to operands of type 'string' and 'int'

                              I dont want to use 'null'instaed off " ". and dont want to make int as nullable. can anybdy help? Regrds, Reta

                              V Offline
                              V Offline
                              V 0
                              wrote on last edited by
                              #14

                              bigphish wrote:

                              wch is wrking fine with Vb..

                              Which shows VB is absolute rubbish. You cannot compare apples with oranges. You need to convert one of the two to the other type. (depending on what you want). you could check out int.TryParse or the ToString method or the Convert classes. Each has it's own advantages. oh and please. Loose the text speech and check your post for spelling before submitting. It's nicer for us the read :-D

                              V.

                              B 1 Reply Last reply
                              0
                              • V V 0

                                bigphish wrote:

                                wch is wrking fine with Vb..

                                Which shows VB is absolute rubbish. You cannot compare apples with oranges. You need to convert one of the two to the other type. (depending on what you want). you could check out int.TryParse or the ToString method or the Convert classes. Each has it's own advantages. oh and please. Loose the text speech and check your post for spelling before submitting. It's nicer for us the read :-D

                                V.

                                B Offline
                                B Offline
                                bigphish
                                wrote on last edited by
                                #15

                                Okie.. sorry if my code was unclear..:( I am converting some code from vb to c#, wch will take some values from Oracle database and will insert in to a MS access database..so I cant view the oracle database wch is in some other server.legseq field in oracle database is taking data dynamically , which can be empty also some times. so while inserting in to Access database I need to check if the value of legseq is empty.if it is empty i need to make it Zero before inserting. hope its clear now..

                                B V W 3 Replies Last reply
                                0
                                • B bigphish

                                  Okie.. sorry if my code was unclear..:( I am converting some code from vb to c#, wch will take some values from Oracle database and will insert in to a MS access database..so I cant view the oracle database wch is in some other server.legseq field in oracle database is taking data dynamically , which can be empty also some times. so while inserting in to Access database I need to check if the value of legseq is empty.if it is empty i need to make it Zero before inserting. hope its clear now..

                                  B Offline
                                  B Offline
                                  BobJanova
                                  wrote on last edited by
                                  #16

                                  You should almost certainly do this with DataAdapters and other useful things in System.Data instead of trying to convert the VB code verbatim. Handling of database nulls is something which I suspect will not work the same in the two environments anyway.

                                  1 Reply Last reply
                                  0
                                  • B bigphish

                                    Okie.. sorry if my code was unclear..:( I am converting some code from vb to c#, wch will take some values from Oracle database and will insert in to a MS access database..so I cant view the oracle database wch is in some other server.legseq field in oracle database is taking data dynamically , which can be empty also some times. so while inserting in to Access database I need to check if the value of legseq is empty.if it is empty i need to make it Zero before inserting. hope its clear now..

                                    V Offline
                                    V Offline
                                    V 0
                                    wrote on last edited by
                                    #17

                                    Better :-) My guess is that you need to check before you have the 'int leqseq' value. Here are some things to look at: - Can the value leqseq be null ? if so a nullable int (int?) or checking agains DBNull.Value is the best way to go. then you can do this: for int?

                                    if(leqseq.HasValue){
                                    //value is not null, you can get the value with leqseq.Value safely.
                                    }
                                    else{
                                    //value is null, handle here
                                    }

                                    for checking against DBull.Value - do you have a datatable, dataset, resultset somewhere? in that case you can compare the database value against DBNull.Value.

                                    if(mytable.Rows[rowindex]["columnname"] != DBNull.Value){
                                    //value is not null
                                    }
                                    else{
                                    //value is null, handle here
                                    }

                                    If the value leqseq cannot be null in the database you need to test if has an invalid 'value' like 0 or -1 or something, but this is bad database design. In that case you don't need a string either, but just compare against the 'fake' value. Hope this helps.

                                    V.

                                    1 Reply Last reply
                                    0
                                    • B bigphish

                                      Okie.. sorry if my code was unclear..:( I am converting some code from vb to c#, wch will take some values from Oracle database and will insert in to a MS access database..so I cant view the oracle database wch is in some other server.legseq field in oracle database is taking data dynamically , which can be empty also some times. so while inserting in to Access database I need to check if the value of legseq is empty.if it is empty i need to make it Zero before inserting. hope its clear now..

                                      W Offline
                                      W Offline
                                      Wayne Gaylard
                                      wrote on last edited by
                                      #18

                                      I don't know how you are retrieving the values from the Oracle database, but ADO.Net DataReaders have an IsDBNull property, which can be used to check if a result is null, so you can do something like this :-

                                      count = dr.IsDBNull(0) ? 0 : (int)dr[0];

                                      and that will give you a 0 if the result is null, otherwise will return the value (assumes your data reader is dr).

                                      When I was a coder, we worked on algorithms. Today, we memorize APIs for countless libraries — those libraries have the algorithms - Eric Allman

                                      B 1 Reply Last reply
                                      0
                                      • B bigphish

                                        hi.. am new to c#.. am getting an error below code..wch is wrking fine with Vb..

                                        int legseq = 0;
                                        if (legseq == " ")
                                        {
                                        legseq = 0;
                                        }

                                        Error: Operator '==' cannot be applied to operands of type 'string' and 'int'

                                        I dont want to use 'null'instaed off " ". and dont want to make int as nullable. can anybdy help? Regrds, Reta

                                        M Offline
                                        M Offline
                                        Mc_Topaz
                                        wrote on last edited by
                                        #19

                                        The compile error message says it all. You cannot implicit compare integer datatype to string datatype To get your code to compile you can use:

                                        int legseq = 0;
                                        if (legseq.ToStirng() == " ")
                                        {
                                        legseq = 0;
                                        }

                                        However, that if-statement will ALWAYS be false and legseq will never be assigned to 0. What you're trying to do make no sense. You might be looking for:

                                        int legseq = 0;
                                        if (legseq != 0)
                                        {
                                        legseq = 0;
                                        }

                                        For me it looks like you have not looked and though over your program flow in your code.

                                        /Steffe

                                        1 Reply Last reply
                                        0
                                        • W Wayne Gaylard

                                          I don't know how you are retrieving the values from the Oracle database, but ADO.Net DataReaders have an IsDBNull property, which can be used to check if a result is null, so you can do something like this :-

                                          count = dr.IsDBNull(0) ? 0 : (int)dr[0];

                                          and that will give you a 0 if the result is null, otherwise will return the value (assumes your data reader is dr).

                                          When I was a coder, we worked on algorithms. Today, we memorize APIs for countless libraries — those libraries have the algorithms - Eric Allman

                                          B Offline
                                          B Offline
                                          bigphish
                                          wrote on last edited by
                                          #20

                                          yes .Thanks alot for the help :)

                                          1 Reply Last reply
                                          0
                                          Reply
                                          • Reply as topic
                                          Log in to reply
                                          • Oldest to Newest
                                          • Newest to Oldest
                                          • Most Votes


                                          • Login

                                          • Don't have an account? Register

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • World
                                          • Users
                                          • Groups