Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
406 views
in Technique[技术] by (71.8m points)

c# - Error on if statement - cannot implicitly convert type to 'bool'

I am having an issue converting type. I was trying code like this (minimal, detailed code later):

string cityType = "City1";
int listingsToSearch = 42;
if (cityType = "City1") // <-- error on this line
{
    listingsToSearch = 1;
}

But "if" statement to convert the cities but I keep getting:

cannot implicitly convert type 'string' to 'bool'


What I'm trying to achieve: I have a search engine that has a textbox for the search text and two radio buttons for the search location ( IE City1 or City2 )

When I receive the search text and radio buttons they are in the form of a string

string thesearchtext, thecitytype;
thesearchtext = HttpContext.Current.Request.QueryString["s"].ToString();
thecitytype = HttpContext.Current.Request.QueryString["bt"].ToString();

When I receive the cities radiobutton they will be in the format of "city1" or "city2".

What i need to do is convert the city radiobuttons into an int so that I can use them in my search dataset. I need to convert "city" to integer 1 and "city2" to integer 2.

I understand this is probably a simple type conversion however I just cannot figure it out. So far code with if gives me error above:

int listingsToSearch;
if (thecitytype = "City1")
{
    listingsToSearch = Convert.ToInt32(1);
}
else
{
    listingsToSearch = Convert.ToInt32(2);
}
Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

c# equality operator is == and not =:

if (thecitytype == "City1")

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...