if语句出错 - 无法将类型隐式转换为'bool'

时间:2022-05-01 16:05:00

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'

不能隐式地将类型'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.

我需要做的是将城市radiobuttons转换为int,以便我可以在我的搜索数据集中使用它们。我需要将“city”转换为整数1,将“city2”转换为整数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);
}

2 个解决方案

#1


c# equality operator is == and not =:

c#相等运算符是==而不是=:

if (thecitytype == "City1")

#2


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等 - 下面的测试演示该代码将尝试读取单选按钮标签中“City”之后开始的整数。另外,请查看您可以在主代码中编写的内容

[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

#1


c# equality operator is == and not =:

c#相等运算符是==而不是=:

if (thecitytype == "City1")

#2


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等 - 下面的测试演示该代码将尝试读取单选按钮标签中“City”之后开始的整数。另外,请查看您可以在主代码中编写的内容

[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