Operator '==' cannot be applied to operands of type 'string' and 'int'
-
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
-
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
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
-
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
-
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
-
Thnks for the reply Luc. will the below code can give the same result?
int? legseq = 0;
if (legseq == null)
{
legseq = 0;
}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
-
Thnks for the reply Luc. will the below code can give the same result?
int? legseq = 0;
if (legseq == null)
{
legseq = 0;
}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
-
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?'"
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.
-
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.
-
Yes, I'm sure. You changed the type of
test
as well as its initialization. Of course it's going to be different.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.
-
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.
-
I guess I could have been clearer.. And I'm still wondering what he actually wants to do.
He absolutely positively undeniably wants to make sure
legseq
is zero, I'd say. :)Luc Pattyn [My Articles] Nil Volentibus Arduum
-
I guess I could have been clearer.. And I'm still wondering what he actually wants to do.
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
-
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
-
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
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.
-
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.
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..
-
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..
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.
-
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..
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.
-
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..
I don't know how you are retrieving the values from the Oracle database, but
ADO.Net DataReaders
have anIsDBNull
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
-
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
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
-
I don't know how you are retrieving the values from the Oracle database, but
ADO.Net DataReaders
have anIsDBNull
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