C# if 语句出错 - 无法将类型隐式转换为“bool”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/871530/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Error on if statement - cannot implicitly convert type to 'bool'
提问by Jason
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:
但是“if”语句转换城市,但我不断收到:
cannot implicitly convert type 'string' to 'bool'
不能将类型“string”隐式转换为“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 )
我想要实现的目标:我有一个搜索引擎,它有一个用于搜索文本的文本框和两个用于搜索位置的单选按钮(IE City1 或 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".
当我收到城市单选按钮时,它们的格式将是“city1”或“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
.
我需要做的是将城市单选按钮转换为 int,以便我可以在我的搜索数据集中使用它们。我需要转换"city"
为 integer1
和"city2"
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:
我知道这可能是一个简单的类型转换,但是我无法弄清楚。到目前为止的代码if
给了我上面的错误:
int listingsToSearch;
if (thecitytype = "City1")
{
listingsToSearch = Convert.ToInt32(1);
}
else
{
listingsToSearch = Convert.ToInt32(2);
}
采纳答案by Rick Hochstetler
回答by dplante
Here is some code in you can use with NUnit that demonstrates another technique for calculating listingToSearch - You will also notice that with this technique, you won't need to add extract if/else, etc as you add more cities - the test below demonstrates that the code will just try to read integer starting after "City" in the radio button label. Also, see the very bottom for what you could write in your main code
下面是一些可以与 NUnit 一起使用的代码,它演示了另一种计算 listingToSearch 的技术 - 您还会注意到,使用这种技术,当您添加更多城市时,您不需要添加提取 if/else 等 - 下面的测试演示代码将尝试从单选按钮标签中的“城市”之后开始读取整数。另外,请参阅最底部的内容,了解您可以在主代码中编写的内容
[Test]
public void testGetCityToSearch()
{
// if thecitytype = "City1", listingToSearch = 1
// if thecitytype = "City2", listingToSearch = 2
doParseCity(1, "City1");
doParseCity(2, "City2");
doParseCity(20, "City20");
}
public void doParseCity(int expected, string input )
{
int listingsToSearch;
string cityNum = input.Substring(4);
bool parseResult = Int32.TryParse(cityNum, out listingsToSearch);
Assert.IsTrue(parseResult);
Assert.AreEqual(expected, listingsToSearch);
}
In your regular code you can just write:
在您的常规代码中,您只需编写:
string thecitytype20 = "City20";
string cityNum20 = thecitytype20.Substring(4);
bool parseResult20 = Int32.TryParse(cityNum20, out listingsToSearch);
// parseResult20 tells you whether parse succeeded, listingsToSearch will give you 20