Compare a value against values in a list C#/VB.NET.

huangapple go评论52阅读模式
英文:

Compare a value against values in a list C#/VB.NET

问题

以下是翻译好的部分:

我有一个由用户插入的值,我想要将其与我编码的一组十进制值进行比较。我想遍历列表中的每个值,看看用户输入的值是否大于或小于列表中的特定值,如果是的话,获取该值的索引。

这是我到目前为止编写的代码,从逻辑上讲,我应该能够获取项目的索引,因为代码能够很好地获取项目本身,所以我知道它有效,但是IndexOf命令返回一个"-1"的值,这意味着它在列表中找不到该值。

有人可以帮助我吗?

英文:

I have a value that is inserted by the user that i want to compare against a list of decimal values that i have coded in. I want to go through every value in that list to see if that value typed by the user is greater or less than that specific value in list and if so get the index of that value.

For i As Decimal = 0 To values.Count - 1
   If user_textbox.Text > values.item(i) Then
      test.Text = values.IndexOf(i)
   End If
Next i

This is the code i got up to this point, logically i should get the index of the item because the code snatches up the item itself just fine so i know it works, however the indexof command is returning a "-1" value which means it does not find the value in the list.

Could anyone help me out with this?

答案1

得分: 1

以下是翻译后的代码部分:

试一试它在列表中找到第一个匹配项并进行字符串到十进制转换一次

Dim lookFor As Decimal = 0
如果 Decimal.TryParse(user_textbox.Text, lookFor) Then
    对于 i As Integer = 0 到 values.Count - 1
        如果 lookFor > values.Item(i) Then
            test.Text = values(i).ToString
            退出 For '<<<<<<<<<<<<<<<<<<<<<<<<<<<<
        End If
    下一个 i
否则
    '错误,不是十进制值
结束 If

请注意,我只提供了代码部分的翻译,没有包括问题或其他内容。如果您需要进一步的翻译或有其他问题,请随时提出。

英文:

Give this a try. It finds the FIRST match in the list and does the string to decimal conversion once.

    Dim lookFor As Decimal = 0
    If Decimal.TryParse(user_textbox.Text, lookFor) Then
        For i As Integer = 0 To values.Count - 1
            If lookFor &gt; values.Item(i) Then
                test.Text = values(i).ToString
                Exit For &#39;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;
            End If
        Next i
    Else
        &#39;error not decimal value
    End If

答案2

得分: 0

Here is the translated code part:

好的您的循环计数器应该是一个整数例如

    For i As Integer = 0 To values.Count - 1
       If user_textbox.Text > values.Item(i) Then
          test.Text = i
       End If
    Next i

这样就可以修复您的计数器/for循环

但是对于上面的情况您需要/希望/应该/最好使用索引循环计数器作为结果请注意我们不使用Mylist.Indexof()也不需要它

不过您可以使用 "index of"但必须传递列表中的一个现有值才能使其工作

所以您可以这样做

        test.Text = -1
        For Each OneValue As Decimal In MyList
            If CType(user_TextBox.Text, Decimal) > OneValue Then
                test.Text = MyList.IndexOf(OneValue)
            End If
        Next

我会"建议"使用第一个示例因为它运行得更快因为我们不必重新搜索和重新查找列表中刚刚测试过的 "OneValue"使用indexOF在CPU方面成本高昂),因为在第一个示例循环中我们已经具有了我们的循环计数器的列表的 "索引"

由于我不喜欢使用 ".count"?(更喜欢使用for each?)

我通常会这样编写代码

        test.Text = -1
        Dim iFound As Integer = 0
        For Each OneValue As Decimal In MyList
            If CType(user_TextBox.Text, Decimal) > OneValue Then
                test.Text = iFound
            End If
            iFound += 1
        Next

for each 保存了一个变量的声明但然后我们不得不声明一个计数器变量所以我们实际上并没有节省太多

如果您不仅仅是要循环遍历所有值那么我认为第一个for/next示例就可以了

就像这样

        test.Text = -1
        For i As Integer = 0 To MyList.Count - 1
            If CType(user_TextBox.Text, Decimal) > MyList.Item(i) Then
                test.Text = i
            End If
        Next i

Is there anything else you would like to know or translate?

英文:

Well, your loop counter should be a integer, such as

For i As Integer = 0 To values.Count - 1
If user_textbox.Text &gt; values.item(i) Then
test.Text = i
End If
Next i

so, that will fix up your counter/for loop.

However, for above, you need/want/should/good idea to use the index loop counter as the result. Note how we do NOT use Mylist.Indexof(), and it not required.

However, you can use "index of", but you MUST pass a existing value from the list for this to work.

So, you could do it this way:

    test.Text = -1
For Each OneValue As Decimal In MyList
If CType(user_TextBox.Text, Decimal) &gt; OneValue Then
test.Text = MyList.IndexOf(OneValue)
End If
Next

I would "suggest" that the first example would run MUCH faster, since we don't have to re-search and re-find the "OneValue" we just tested out of the list again (using indexOF is high cost CPU wise), since with the first example loop, we already have the "index" of the list with our loop counter.

Since I don't like to use ".count" ? (and prefer for each?)

I'll often write code this way:

    test.Text = -1
Dim iFound As Integer = 0
For Each OneValue As Decimal In MyList
If CType(user_TextBox.Text, Decimal) &gt; OneValue Then
test.Text = iFound
End If
iFound += 1
Next

The For Each saves declare of a variable, but then we had to declare a counter var, so we really don't save much.

Since you not JUST looking to loop over all values? Then I think the first for/next example is fine.

this one:

    test.Text = -1
For i As Integer = 0 To MyList.Count - 1
If CType(user_TextBox.Text, Decimal) &gt; MyList.Item(i) Then
test.Text = i
End If
Next i

答案3

得分: 0

以下是翻译好的部分:

在一些尝试和错误之后,这段代码最终起作用:

For i As Decimal = 0 To values.Count - 1
    If user_textbox.Text > values.Item(i) Then
        test.Text = values.IndexOf(values(i))
    End If
Next i

我没有传递列表中的实际值,但这将起作用。

英文:

After some trial and error, this was the code that ended up working:

For i As Decimal = 0 To values.Count - 1
If user_textbox.Text &gt; values.item(i) Then
test.Text = values.IndexOf(values(i))
End If
Next i

I wasn't passing the actual value in the list, but this will do the trick.

huangapple
  • 本文由 发表于 2023年5月15日 02:29:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/76249077.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定