user3314640 user3314640 - 1 month ago 9
C# Question

TryParse Invalid Arguments

I am trying to run a NUnit test searching for numbers within a string. I'm using TryParse but it gave me a this error: Error1: The best overloaded method match for 'int.TryParse(string, out int)' has some invalid arguments.

Could you guys tell me what I'm doing wrong?

[Test]
public void ThenSearchForBrittanyShouldFindOneUser()
{
var searchresult = LuceneFullTextSearch.SuggestSimilar("brit123", 1).ToArray();


int num = 0;
foreach (var result in searchresult)
{

if (result = int.TryParse(searchresult, out num))
{
searchresult.ShouldNotBeEmpty();
searchresult.ShouldContain("Brittany");
searchresult.Should().Count.NoMoreThan(1);
}
else
{
searchresult.ShouldNotBeEmpty();
searchresult.Equals(num);

Answer

searchresult is an array, not a string; you want result there instead.

Also, you can't have an assignment in an if conditional, so your syntax is wrong anyway. TryParse already returns a bool, so you should try:

    if (int.TryParse(result, out num))
    {
         ...
Comments