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

后端 未结 2 414
温柔的废话
温柔的废话 2020-11-27 23:12

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

string cityType = \"City1\";
int listingsToSearch = 42;
if          


        
相关标签:
2条回答
  • 2020-11-27 23:27

    c# equality operator is == and not =:

    if (thecitytype == "City1")
    
    0 讨论(0)
  • 2020-11-27 23:50

    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

    [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
    
    0 讨论(0)
提交回复
热议问题