if语句出错 - 无法将类型隐式转换为'bool' [英] 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;
}
但是if语句转换城市但我一直得到:
But "if" statement to convert the cities but I keep getting:
无法将类型'string'隐式转换为'bool'
cannot implicitly convert type 'string' to 'bool'
我想要实现的目标:我有一个搜索引擎,其中包含搜索文本的文本框和搜索位置的两个单选按钮(IE City1或City2)
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();
当我收到城市单选按钮时,它们的格式为city1或city2。
When I receive the cities radiobutton they will be in the format of "city1" or "city2".
我需要做的是将城市单选按钮转换为int,以便我可以在搜索数据集中使用它们。我需要将city
转换为整数 1
和city2
到整数 2
。
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);
}
推荐答案
c#等于运算符是 ==
和不是 =
:
if (thecitytype == "City1")
这篇关于if语句出错 - 无法将类型隐式转换为'bool'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!