这是为什么的IndexOf调用返回-1?这是、IndexOf

2023-09-07 10:15:17 作者:初吻给了烟

据我所知,IndexOf返回-1时,未找到该参数,但是这并没有道理给我。

我有这个code,它迭代在我的表单中的所有DevEx preSS复选框,检查,看看有什么自己的标签,并试图发现它在传递给方法的过滤器参数。如果标签在过滤器中发现问题,应检查复选框。它总是等同于虚假的。

 公共无效FilterChanged(Control.ControlCollection控制,串过滤器)
    {
        过滤器= filter.Replace([的String.Empty);
        过滤器= filter.Replace(],的String.Empty);

        的foreach(对照控制控制)
        {
            如果(控制CheckEdit和放大器;&安培;!control.Tag = NULL)
            {
                变种C =(CheckEdit)控制;
                VAR CFILTER = c.Tag.ToString();
                CFILTER = cFilter.Replace((,的String.Empty);
                CFILTER = cFilter.Replace()的String.Empty);

                如果(filter.ToUpper()的IndexOf(c.Tag.ToString()ToUpper的())> = 0)
                    c.Checked = TRUE;
                其他
                    c.Checked = FALSE;
            }
        }
    }
 

我将在我的内心如果语句的断点,并在我的即时窗口我输入以下内容:

filter.ToUpper()的IndexOf(c.Tag.ToString()。ToUpper的()) = -1

filter.ToUpper() =FILE NOT LIKE'%VERSIONINFO.CS%'

cFilter.ToUpper() =FILE NOT LIKE'%VERSIONINFO.CS%'

这些看起来像pretty的多少完全一样的东西,所以应该不是在返回0?

我不能使用等于,因为过滤器可能包括多个条款,因此不会是平等的。

解决方案

  cFilter.ToUpper()=FILE NOT LIKE'%VERSIONINFO.CS%'
 

  

这些看起来像pretty的多少完全一样的东西,所以应该不是在返回0?

但是,你正在使用 c.Tag.ToString()而不是 cFilter.ToUpper()

因此​​,这应能按预期:

 如果(filter.ToUpper()的IndexOf(cFilter.ToUpper())> = 0)
    c.Checked = TRUE;
其他
    c.Checked = FALSE;
 
浏览器出现Index of

请注意,你应该使用 StringComparison.OrdinalIgnoreCase 在代替的 的IndexOf

  c.Checked = filter.IndexOf(CFILTER,StringComparison.OrdinalIgnoreCase)> = 0;
 

I understand that IndexOf returns -1 when the parameter isn't found, but this doesn't make sense to me.

I have this code that iterates over all the DevExpress Checkboxes on my form, checks to see what their tag is, and tries to find it in the "filter" parameter that was passed to the method. If the tag is found in the filter, it should check that checkbox. It is always equating to false.

    public void FilterChanged(Control.ControlCollection controls, string filter)
    {
        filter = filter.Replace("[", String.Empty);
        filter = filter.Replace("]", String.Empty);

        foreach (Control control in controls)
        {
            if (control is CheckEdit && control.Tag != null)
            {
                var c = (CheckEdit)control;
                var cFilter = c.Tag.ToString();
                cFilter = cFilter.Replace("(", String.Empty);
                cFilter = cFilter.Replace(")", String.Empty);

                if (filter.ToUpper().IndexOf(c.Tag.ToString().ToUpper()) >= 0)
                    c.Checked = true;
                else
                    c.Checked = false;
            }
        }
    }

I set a breakpoint on my inner IF statement, and in my Immediate Window I entered the following:

filter.ToUpper().IndexOf(c.Tag.ToString().ToUpper()) = -1

filter.ToUpper() = "FILE NOT LIKE '%VERSIONINFO.CS%'"

cFilter.ToUpper() = "FILE NOT LIKE '%VERSIONINFO.CS%'"

Those look like pretty much the exact same thing, so shouldn't it be returning 0?

I cannot use equals because the filter might include multiple clauses, and thus wouldn't be equal.

解决方案

cFilter.ToUpper() = "FILE NOT LIKE '%VERSIONINFO.CS%'"

Those look like pretty much the exact same thing, so shouldn't it be returning 0?

But you are using c.Tag.ToString() instead of cFilter.ToUpper().

So this should work as expected:

if (filter.ToUpper().IndexOf(cFilter.ToUpper()) >= 0)
    c.Checked = true;
else
    c.Checked = false;

Note that you should use StringComparison.OrdinalIgnoreCase instead in IndexOf

c.Checked = filter.IndexOf(cFilter, StringComparison.OrdinalIgnoreCase) >= 0;