if 语句出错 - 无法将类型隐式转换为“bool" [英] Error on if statement - cannot implicitly convert type to 'bool'

查看:343
本文介绍了if 语句出错 - 无法将类型隐式转换为“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.

我知道这可能是一个简单的类型转换,但我就是想不通.到目前为止,if 的代码给了我上面的错误:

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆